LoginSignup
2
3

More than 5 years have passed since last update.

choroplethr - Vignettes - e) 国家単位のコロプレス図

Last updated at Posted at 2014-12-09

Rのパッケージchoroplethrのビネットの翻訳です.
( 原文: http://cran.r-project.org/web/packages/choroplethr/index.html )


e) 国家単位のコロプレス図

country_choropleth関数で国のコロプレス図を作成することができます.

library(choroplethr)

?df_pop_country
data(df_pop_country)

?country_choropleth
country_choropleth(df_pop_country)
## Warning in self$bind(): The following regions were missing and are being
## set to NA: namibia, western sahara, taiwan, antarctica, kosovo

pop-country.png

上で示したように,country_choroplethに必須の引数はデータフレームだけです.
オプション引数は?country_choroplethとタイプすれば見ることができます.

この地図はchoroplethrの新たな機能の例示になっています.
"value"がない場合(南極の人口って何人でしょうか?),その地域は黒く表示されます

データ要件

country_choropleth関数に渡すデータフレームは"region"と"value"という名前の列を持っている必要があります.
"region"列の要素はchoroplethrが使用している地図の地域名と厳密に一致している必要があります.
これらの地域名はcountry.regionsオブジェクトで定義されています.

library(choroplethrMaps)

?country.regions
data(country.regions)
head(country.regions)
##        region iso2c
## 1 afghanistan    AF
## 2      angola    AO
## 3  azerbaijan    AZ
## 4     moldova    MD
## 5  madagascar    MG
## 6      mexico    MX

choroplethrを使用するには,country.regionsの"region"列の命名規則に従わなければなりません.
つまり,アルファベット小文字でフルネームの国名を使う必要があります.

データの探索

country_choropleth 関数にはデータの探索を助ける二つの引数が用意されています.num_colorszoom引数です.
num_colorsの値はデフォルトでは7です.これは7種類の色が使用され,それぞれの色で等しい数の州が塗り分けられるということです.
num_colorsの有効な値は1から7の間の整数です.
num_colorsが1の場合は連続的なカラースケールが使用されます.
zoomはデフォルトではNULLであり,全ての州が表示されます.
zoomには有効な州の名前のベクトルを指定することができます.

例として,choroplethrでアメリカ,カナダ,メキシコの人口を表示してみましょう.

country_choropleth(df_pop_country,
                 title      = "2012 Population Estimates",
                 legend     = "Population",
                 num_colors = 1,
                 zoom       = c("united states of america", "mexico", "canada"))

pop-country-zoom.png

高度なオプション

上で例示したオプション引数によるカスタマイズ以上のことがしたい場合は,
CountryChoroplethオブジェクトを作成する必要があります.
choroplethrはオブジェクト指向プログラミングを活用するためにR6を使っています.
以下の例ではパレットをカスタマイズするためにChoropleth基底オブジェクトを
ggplot2_scaleを使って拡張しています.

library(ggplot2)

choro = CountryChoropleth$new(df_pop_country)
choro$title = "2012 Population Estimates2012 Election Results"
choro$ggplot_scale = scale_fill_brewer(name="Population", palette=2)
choro$render()
## Warning in self$bind(): The following regions were missing and are being
## set to NA: namibia, western sahara, taiwan, antarctica, kosovo

pop-country-green.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3