Rで解析:日本の行政地区をプロットするのに便利です「jpndistrict」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

日本の行政地区をプロットするのに便利なパッケージの紹介です。

パッケージバージョンは0.3.0。windows 10のR version 3.4.3で動作を確認しています。

スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("jpndistrict")

コマンドの紹介

詳細はコマンド、パッケージのヘルプを確認してください。

#パッケージの読み込み
library("jpndistrict")
#install.packages("devtools")
#sfオブジェクトを利用するためにgithubからggplot2をインストール
#devtools::install_github("tidyverse/ggplot2")
library("ggplot2")

#shinyでインタラクティブに行政区間を確認:district_viewerコマンド
district_viewer()

#経度緯度から行政区都道府県市名の情報を取得:find_cityコマンド
GetCity <- find_city(longitude = 137.5993, latitude = 35.36358)
#&#30906;&#35469;
GetCity
Simple feature collection with 1 feature and 3 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 137.2497 ymin: 35.22276 xmax: 137.604 ymax: 35.54718
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
# A tibble: 1 x 4
  prefecture city_code   city          geometry
       &lt;chr&gt;     &lt;chr&gt;  &lt;chr&gt; &lt;S3: sfc_POLYGON&gt;
1     &#23696;&#38428;&#30476;     21210 &#24693;&#37027;&#24066; &lt;S3: sfc_POLYGON&gt;
#ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;
ggplot(GetCity, aes(geometry = geometry)) +
geom_sf(col = "red", fill = "gray") +
theme_bw()
#plot&#12467;&#12510;&#12531;&#12489;
plot(GetCity$geometry, col = "gray")
########

#&#32076;&#24230;&#32239;&#24230;&#12363;&#12425;&#34892;&#25919;&#21306;&#37117;&#36947;&#24220;&#30476;&#12398;&#24773;&#22577;&#12434;&#21462;&#24471;:find_pref&#12467;&#12510;&#12531;&#12489;
GetPref <- find_pref(longitude = 137.5993, latitude = 35.36358)
#&#32080;&#26524;
GetPref
Simple feature collection with 1 feature and 2 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 136.2751 ymin: 35.13267 xmax: 137.6542 ymax: 36.46616
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
# A tibble: 1 x 3
  pref_code prefecture          geometry
      &lt;chr&gt;      &lt;chr&gt; &lt;S3: sfc_POLYGON&gt;
1        21     &#23696;&#38428;&#30476; &lt;S3: sfc_POLYGON&gt;
#ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;
ggplot(GetPref, aes(geometry = geometry)) +
geom_sf(col = "red", fill = "gray") +
theme_bw()
#plot&#12467;&#12510;&#12531;&#12489;
plot(GetPref$geometry, col = "gray")  
########  

#&#32076;&#24230;&#32239;&#24230;&#12395;&#21547;&#12414;&#12428;&#12427;&#34892;&#25919;&#21306;&#37117;&#36947;&#24220;&#30476;&#12398;&#24773;&#22577;&#12434;&#21462;&#24471;:find_prefs&#12467;&#12510;&#12531;&#12489;
GetPrefs <- find_prefs(longitude = 137.5993, latitude = 35.36358)
#&#32080;&#26524;
GetPrefs
# A tibble: 3 x 4
  pref_code meshcode_80km prefecture region
      &lt;chr&gt;         &lt;dbl&gt;     &lt;fctr&gt;  &lt;chr&gt;
1        20          5337     &#38263;&#37326;&#30476;   &#20013;&#37096;
2        21          5337     &#23696;&#38428;&#30476;   &#20013;&#37096;
3        23          5337     &#24859;&#30693;&#30476;   &#20013;&#37096;
#######

#&#34892;&#25919;&#21306;&#37117;&#36947;&#24220;&#30476;&#12398;jis_code,&#32076;&#24230;&#32239;&#24230;&#31561;&#12398;&#12487;&#12540;&#12479;&#12475;&#12483;&#12488;:jpnprefs&#12467;&#12510;&#12531;&#12489;
jpnprefs
# A tibble: 47 x 7
jis_code prefecture  capital region major_island capital_latitude capital_longitude
&lt;chr&gt;     &lt;fctr&gt;    &lt;chr&gt;  &lt;chr&gt;        &lt;chr&gt;            &lt;dbl&gt;             &lt;dbl&gt;
1    01     &#21271;&#28023;&#36947;   &#26413;&#24140;&#24066; &#21271;&#28023;&#36947;       &#21271;&#28023;&#36947;         43.06208          141.3544
2    02     &#38738;&#26862;&#30476;   &#38738;&#26862;&#24066;   &#26481;&#21271;         &#26412;&#24030;         40.82200          140.7472
3    03     &#23721;&#25163;&#30476;   &#30427;&#23713;&#24066;   &#26481;&#21271;         &#26412;&#24030;         39.70197          141.1544
4    04     &#23470;&#22478;&#30476;   &#20185;&#21488;&#24066;   &#26481;&#21271;         &#26412;&#24030;         38.26811          140.8693
5    05     &#31179;&#30000;&#30476;   &#31179;&#30000;&#24066;   &#26481;&#21271;         &#26412;&#24030;         39.71975          140.1022
6    06     &#23665;&#24418;&#30476;   &#23665;&#24418;&#24066;   &#26481;&#21271;         &#26412;&#24030;         38.25539          140.3395
7    07     &#31119;&#23798;&#30476;   &#31119;&#23798;&#24066;   &#26481;&#21271;         &#26412;&#24030;         37.76089          140.4734
8    08     &#33576;&#22478;&#30476;   &#27700;&#25144;&#24066;   &#38306;&#26481;         &#26412;&#24030;         36.36583          140.4711
9    09     &#26627;&#26408;&#30476; &#23431;&#37117;&#23470;&#24066;   &#38306;&#26481;         &#26412;&#24030;         36.55503          139.8828
10   10     &#32676;&#39340;&#30476;   &#21069;&#27211;&#24066;   &#38306;&#26481;         &#26412;&#24030;         36.38936          139.0633
# ... with 37 more rows
#######

#jis_code&#12391;&#24773;&#22577;&#12434;&#21462;&#24471;:jpn_cities&#12467;&#12510;&#12531;&#12489;
GetCities <- jpn_cities(jis_code = 21)
#&#34892;&#25919;&#21306;&#24066;&#30010;&#26449;&#21517;&#12434;&#30906;&#35469;
GetCities$city
[1] "&#23696;&#38428;&#24066;"          "&#22823;&#22435;&#24066;"          "&#39640;&#23665;&#24066;"          "&#22810;&#27835;&#35211;&#24066;"        "&#38306;&#24066;"            "&#20013;&#27941;&#24029;&#24066;"        "&#32654;&#28611;&#24066;"         
[8] "&#29790;&#28010;&#24066;"          "&#32701;&#23798;&#24066;"          "&#24693;&#37027;&#24066;"          "&#32654;&#28611;&#21152;&#33538;&#24066;"      "&#22303;&#23696;&#24066;"          "&#21508;&#21209;&#21407;&#24066;"        "&#21487;&#20816;&#24066;"         
[15] "&#23665;&#30476;&#24066;"          "&#29790;&#31298;&#24066;"          "&#39131;&#39464;&#24066;"          "&#26412;&#24035;&#24066;"          "&#37089;&#19978;&#24066;"          "&#19979;&#21570;&#24066;"          "&#28023;&#27941;&#24066;"         
[22] "&#32701;&#23798;&#37089; &#23696;&#21335;&#30010;"   "&#32701;&#23798;&#37089; &#31520;&#26494;&#30010;"   "&#39178;&#32769;&#37089; &#39178;&#32769;&#30010;"   "&#19981;&#30772;&#37089; &#22402;&#20117;&#30010;"   "&#19981;&#30772;&#37089; &#38306;&#12465;&#21407;&#30010;" "&#23433;&#20843;&#37089; &#31070;&#25144;&#30010;"   "&#23433;&#20843;&#37089; &#36650;&#20043;&#20869;&#30010;"
[29] "&#23433;&#20843;&#37089; &#23433;&#20843;&#30010;"   "&#25558;&#26000;&#37089; &#25558;&#26000;&#24029;&#30010;" "&#25558;&#26000;&#37089; &#22823;&#37326;&#30010;"   "&#25558;&#26000;&#37089; &#27744;&#30000;&#30010;"   "&#26412;&#24035;&#37089; &#21271;&#26041;&#30010;"   "&#21152;&#33538;&#37089; &#22338;&#31069;&#30010;"   "&#21152;&#33538;&#37089; &#23500;&#21152;&#30010;"  
[36] "&#21152;&#33538;&#37089; &#24029;&#36794;&#30010;"   "&#21152;&#33538;&#37089; &#19971;&#23447;&#30010;"   "&#21152;&#33538;&#37089; &#20843;&#30334;&#27941;&#30010;" "&#21152;&#33538;&#37089; &#30333;&#24029;&#30010;"   "&#21152;&#33538;&#37089; &#26481;&#30333;&#24029;&#26449;" "&#21487;&#20816;&#37089; &#24481;&#23913;&#30010;"   "&#22823;&#37326;&#37089; &#30333;&#24029;&#26449;" 
#&#21442;&#32771;&#20363;:&#24066;&#30010;&#26449;&#12434;&#25351;&#23450;&#12375;&#12390;&#12503;&#12525;&#12483;&#12488;
PlotData <- subset(GetCities, GetCities$city == "&#24693;&#37027;&#24066;" | GetCities$city == "&#39131;&#39464;&#24066;")
plot(PlotData$geometry, col = c("gray", "red"))  
#######

出力例

・find_cityコマンド:ggplot2パッケージを利用

・find_prefコマンド

・jpn_citiesコマンド


少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました