Rで解析:ggplot2でデータ付き地図を作成「geofacet」パッケージ

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

オリジナルの地図データにグラフをプロット可能なパッケージの紹介です。地図データさえ用意できれば非常に有効なパッケージだと思います。参考までに日本地図データの作成例とプロット例を紹介します。

参考に気象庁から20220503の毎時の最高気温を取得して、各都道府県にヒートマップをプロットするコマンドも紹介します。

パッケージバージョンは0.2.0。実行コマンドはwindows 11のR version 4.1.3で確認しています。

スポンサーリンク

日本地図データ例

お好みに合わせて、各都道府県の位置を調整してください。

#日本地図データの作成
JapanGrid <- data.frame(
  row = c(1, 3, 4, 5, 6, 7, 8, 4, 5, 6,
          7, 8, 9, 7, 8, 9, 7, 8, 9, 6,
          7, 8, 9, 8, 9, 8, 9, 10, 8, 9,
          8, 9, 8, 9, 11, 12, 11, 12, 9, 9,
          10, 11, 9, 10, 11, 9, 12),
  col = c(16, 16, 16, 16, 16, 16, 16, 15,
          15, 15, 15, 15, 15, 14, 14, 14,
          13, 13, 13, 12, 12, 12, 12, 11,
          11, 10, 10, 10, 9, 9, 8, 8, 7, 7,
          8, 8, 7, 7, 6, 4, 4, 4, 3, 3, 3, 2, 1),
  code = c("&#21271;&#28023;&#36947;", "&#38738;&#26862;&#30476;", "&#23721;&#25163;&#30476;", "&#23470;&#22478;&#30476;", "&#31119;&#23798;&#30476;",
           "&#33576;&#22478;&#30476;", "&#21315;&#33865;&#30476;", "&#31179;&#30000;&#30476;", "&#23665;&#24418;&#30476;", "&#26032;&#28511;&#30476;",
           "&#26627;&#26408;&#30476;", "&#22524;&#29577;&#30476;", "&#26481;&#20140;&#37117;", "&#32676;&#39340;&#30476;", "&#23665;&#26792;&#30476;",
           "&#31070;&#22856;&#24029;&#30476;", "&#23500;&#23665;&#30476;", "&#38263;&#37326;&#30476;", "&#38745;&#23713;&#30476;", "&#30707;&#24029;&#30476;",
           "&#31119;&#20117;&#30476;", "&#23696;&#38428;&#30476;", "&#24859;&#30693;&#30476;", "&#28363;&#36032;&#30476;", "&#19977;&#37325;&#30476;",
           "&#20140;&#37117;&#24220;", "&#22856;&#33391;&#30476;", "&#21644;&#27468;&#23665;&#30476;", "&#20853;&#24235;&#30476;", "&#22823;&#38442;&#24220;",
           "&#40165;&#21462;&#30476;", "&#23713;&#23665;&#30476;", "&#23798;&#26681;&#30476;", "&#24195;&#23798;&#30476;", "&#39321;&#24029;&#30476;", "&#24499;&#23798;&#30476;",
           "&#24859;&#23195;&#30476;", "&#39640;&#30693;&#30476;", "&#23665;&#21475;&#30476;", "&#31119;&#23713;&#30476;", "&#22823;&#20998;&#30476;", "&#23470;&#23822;&#30476;",
           "&#20304;&#36032;&#30476;", "&#29066;&#26412;&#30476;", "&#40575;&#20816;&#23798;&#30476;", "&#38263;&#23822;&#30476;", "&#27798;&#32260;&#30476;"),
  name = c("&#21271;&#28023;&#36947;", "&#38738;&#26862;&#30476;", "&#23721;&#25163;&#30476;", "&#23470;&#22478;&#30476;", "&#31119;&#23798;&#30476;",
           "&#33576;&#22478;&#30476;", "&#21315;&#33865;&#30476;", "&#31179;&#30000;&#30476;", "&#23665;&#24418;&#30476;", "&#26032;&#28511;&#30476;",
           "&#26627;&#26408;&#30476;", "&#22524;&#29577;&#30476;", "&#26481;&#20140;&#37117;", "&#32676;&#39340;&#30476;", "&#23665;&#26792;&#30476;",
           "&#31070;&#22856;&#24029;&#30476;", "&#23500;&#23665;&#30476;", "&#38263;&#37326;&#30476;", "&#38745;&#23713;&#30476;", "&#30707;&#24029;&#30476;",
           "&#31119;&#20117;&#30476;", "&#23696;&#38428;&#30476;", "&#24859;&#30693;&#30476;", "&#28363;&#36032;&#30476;", "&#19977;&#37325;&#30476;",
           "&#20140;&#37117;&#24220;", "&#22856;&#33391;&#30476;", "&#21644;&#27468;&#23665;&#30476;", "&#20853;&#24235;&#30476;", "&#22823;&#38442;&#24220;",
           "&#40165;&#21462;&#30476;", "&#23713;&#23665;&#30476;", "&#23798;&#26681;&#30476;", "&#24195;&#23798;&#30476;", "&#39321;&#24029;&#30476;", "&#24499;&#23798;&#30476;",
           "&#24859;&#23195;&#30476;", "&#39640;&#30693;&#30476;", "&#23665;&#21475;&#30476;", "&#31119;&#23713;&#30476;", "&#22823;&#20998;&#30476;", "&#23470;&#23822;&#30476;",
           "&#20304;&#36032;&#30476;", "&#29066;&#26412;&#30476;", "&#40575;&#20816;&#23798;&#30476;", "&#38263;&#23822;&#30476;", "&#27798;&#32260;&#30476;"))

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#26085;&#26412;&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#20445;&#23384;
write.csv(JapanGrid, "JapanGrid.csv", row.names = FALSE)
########

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

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

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

実行コマンド

詳細はコマンド、パッケージのヘルプを確認してください。体裁はggplot2のコマンドで調整可能です。

#&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#35501;&#12415;&#36796;&#12415;
library("geofacet")
library("ggplot2")
library("tcltk")

#&#20808;&#12395;&#20316;&#25104;&#12375;&#12383;&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#35501;&#12415;&#36796;&#12416;
JapanGrid <- read.csv(paste0(as.character(tkgetOpenFile(title = "&#12501;&#12449;&#12452;&#12523;&#12434;&#36984;&#25246;",
                                                           filetypes = '{"&#12501;&#12449;&#12452;&#12523;" {".*"}}',
                                                           initialfile = c("*.*")))))
########

###&#12487;&#12540;&#12479;&#20363;&#12434;&#20316;&#25104;#####
TestJPN <- data.frame(Year = factor(rep(2015:2017, times = nrow(JapanGrid))),
                      Temp = sample(10:20, size = nrow(JapanGrid)*3, replace = TRUE),
                      Name = rep(JapanGrid[, 4], each = 3))
########

#&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#34920;&#31034;:grid_preview&#12467;&#12510;&#12531;&#12489;
grid_preview(JapanGrid)

#&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#21033;&#29992;&#12375;&#12390;&#12487;&#12540;&#12479;&#12434;&#12503;&#12525;&#12483;&#12488;:facet_geo&#12467;&#12510;&#12531;&#12489;
#&#12487;&#12540;&#12479;&#12398;&#20998;&#21106;&#26041;&#27861;:facets&#12458;&#12503;&#12471;&#12519;&#12531;;facet_wrap&#12467;&#12510;&#12531;&#12489;&#12392;&#21516;&#12376;
#&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#25351;&#23450;:grid&#12458;&#12503;&#12471;&#12519;&#12531;
ggplot(TestJPN, aes(x = Year, y = Temp, group = Name, col = Name)) +
  geom_line(show.legend = FALSE, size = 1.5) +
  facet_geo(facets = ~ Name, grid = JapanGrid) +
  scale_x_discrete(labels = function(x) paste0("'", substr(x, 3, 4))) +
  scale_y_continuous(labels = function(x) format(x, 2)) +
  labs(title = "2015-2017 &#21508;&#37117;&#36947;&#24220;&#30476; &#20206;&#24819;&#24180;&#24179;&#22343;&#27671;&#28201;") +
  theme(title = element_text(colour = "#ffffe0"),
        axis.title = element_text(colour = "#ffffe0"),
        axis.text = element_text(colour = "#ffffe0"),
        strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill = "white"),
        axis.text.y = element_text(size = 5),
        panel.grid = element_blank(), 
        panel.background = element_rect(fill = "lightgray"),
        plot.background = element_rect(fill = "#0a0a0a"))

#&#21442;&#32771;:&#26834;&#12464;&#12521;&#12501;&#12391;&#34920;&#31034;
ggplot(TestJPN, aes(x = Year, y = Temp, group = Name, fill = Name)) +
  geom_col(show.legend = FALSE) +
  facet_geo(facets = ~ Name, grid = JapanGrid) +
  scale_x_discrete(labels = function(x) paste0("'", substr(x, 3, 4))) +
  scale_y_continuous(labels = function(x) format(x, 2)) +
  labs(title = "2015-2017 &#21508;&#37117;&#36947;&#24220;&#30476; &#20206;&#24819;&#24180;&#24179;&#22343;&#27671;&#28201; &#26834;&#12464;&#12521;&#12501;") +
  theme(title = element_text(colour = "#ffffe0"),
        axis.title = element_text(colour = "#ffffe0"),
        axis.text = element_text(colour = "#ffffe0"),
        strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill = "white"),
        axis.text.y = element_text(size = 5),
        panel.grid = element_blank(), 
        panel.background = element_rect(fill = "lightgray"),
        plot.background = element_rect(fill = "#0a0a0a"))

出力例

各グラフはクリックで大きく見ることができます。

・grid_previewコマンド

・地図データを利用してデータをプロット:facet_geoコマンド

・参考:棒グラフで表示

最高気温を気象庁から取得してプロット

気象庁から20220503の毎時の最高気温を取得した例です。データを変えると最高気温以外もプロット可能です。

データを取得

###&#12487;&#12540;&#12479;&#20363;&#12434;&#20316;&#25104;#####
#&#12300;tidyverse&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#35501;&#12415;&#36796;&#12415;
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
#&#37117;&#36947;&#24220;&#30476;&#12434;&#28310;&#20633;
JpanName <- c("&#21271;&#28023;&#36947;", "&#38738;&#26862;&#30476;", "&#23721;&#25163;&#30476;", "&#23470;&#22478;&#30476;", "&#31119;&#23798;&#30476;", "&#33576;&#22478;&#30476;", "&#21315;&#33865;&#30476;",
              "&#31179;&#30000;&#30476;", "&#23665;&#24418;&#30476;", "&#26032;&#28511;&#30476;", "&#26627;&#26408;&#30476;", "&#22524;&#29577;&#30476;", "&#26481;&#20140;&#37117;", "&#32676;&#39340;&#30476;",
              "&#23665;&#26792;&#30476;", "&#31070;&#22856;&#24029;&#30476;", "&#23500;&#23665;&#30476;", "&#38263;&#37326;&#30476;", "&#38745;&#23713;&#30476;", "&#30707;&#24029;&#30476;", "&#31119;&#20117;&#30476;",
              "&#23696;&#38428;&#30476;", "&#24859;&#30693;&#30476;", "&#28363;&#36032;&#30476;", "&#19977;&#37325;&#30476;", "&#20140;&#37117;&#24220;", "&#22856;&#33391;&#30476;", "&#21644;&#27468;&#23665;&#30476;",
              "&#20853;&#24235;&#30476;", "&#22823;&#38442;&#24220;", "&#40165;&#21462;&#30476;", "&#23713;&#23665;&#30476;", "&#23798;&#26681;&#30476;", "&#24195;&#23798;&#30476;", "&#39321;&#24029;&#30476;",
              "&#24499;&#23798;&#30476;", "&#24859;&#23195;&#30476;", "&#39640;&#30693;&#30476;", "&#23665;&#21475;&#30476;", "&#31119;&#23713;&#30476;", "&#22823;&#20998;&#30476;", "&#23470;&#23822;&#30476;",
              "&#20304;&#36032;&#30476;", "&#29066;&#26412;&#30476;", "&#40575;&#20816;&#23798;&#30476;", "&#38263;&#23822;&#30476;", "&#27798;&#32260;&#30476;")

#&#26178;&#38291;&#25991;&#23383;&#21015;&#12434;&#20316;&#25104;
Hour <- paste0(formatC(0:23, width = 2, flag = "0"), "00")

#&#12487;&#12540;&#12479;&#20445;&#31649;&#29992;&#22793;&#25968;
NewMaxTemp <- data.frame()

for(i in seq(Hour)){
  ###&#27671;&#35937;&#24193;&#12363;&#12425;20220503&#12398;&#27598;&#26178;&#12398;&#26368;&#39640;&#27671;&#28201;&#12434;&#21462;&#24471;#####
  #&#21442;&#32771;:https://www.data.jma.go.jp/obd/stats/data/mdrr/docs/csv_dl_readme.html
  MaxTemp <- read.csv(paste0("https://www.data.jma.go.jp/obd/stats/data/mdrr/tem_rct/alltable/mxtemsadext00_20220503", Hour[i], ".csv"),
                      header = T, fileEncoding = "cp932")
  #&#26368;&#39640;&#27671;&#28201;&#20966;&#29702;
  GetMaxTemp <- NULL
  for(n in 1:47){
    #&#37117;&#36947;&#24220;&#30476;&#12434;&#25277;&#20986;
    GetNameData <- MaxTemp[which(MaxTemp[, 2] %in% grep(JpanName[n], MaxTemp[, 2], value = TRUE)),]
    #&#26368;&#39640;&#27671;&#28201;&#12434;&#38477;&#38918;&#12391;&#20006;&#12403;&#26367;&#12360;
    GetNameData <- GetNameData[order(GetNameData[, 10], decreasing = TRUE),]
    #&#26368;&#39640;&#27671;&#28201;&#12434;&#21462;&#24471;
    GetMaxTemp <- c(GetMaxTemp, GetNameData[1, 10])
  }
  
  HourTemp <- cbind(Hour[i], JpanName, GetMaxTemp)
  NewMaxTemp <- rbind(NewMaxTemp, HourTemp)
  
}

#&#21015;&#21517;&#12434;&#20184;&#19982;
colnames(NewMaxTemp) <- c("Hour", "Name", "MaxTemp")

#&#26368;&#39640;&#27671;&#28201;&#12434;&#25968;&#20516;&#21270;
NewMaxTemp[, 3] <- type.convert(NewMaxTemp[, 3], as.is = TRUE)

#Hour&#12434;factor&#21270;
NewMaxTemp %>%
  mutate(Hour = factor(Hour)) -> NewMaxTemp
########

プロット

#&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#35501;&#12415;&#36796;&#12415;
library("geofacet")

#&#20808;&#12395;&#20316;&#25104;&#12375;&#12383;&#22320;&#22259;&#12487;&#12540;&#12479;&#12434;&#35501;&#12415;&#36796;&#12416;
JapanGrid <- read.csv(paste0(as.character(tkgetOpenFile(title = "&#12501;&#12449;&#12452;&#12523;&#12434;&#36984;&#25246;",
                                                        filetypes = '{"&#12501;&#12449;&#12452;&#12523;" {".*"}}',
                                                        initialfile = c("*.*")))))

#&#12503;&#12525;&#12483;&#12488;
ggplot(NewMaxTemp, aes(x = Hour, y = 1, group = Name, fill = MaxTemp)) +
  geom_tile(show.legend = FALSE, size = 1.5) + 
  facet_geo(facets = ~ Name, grid = JapanGrid) +
  scale_fill_gradient(low = "#6f74a4", high = "red") +
  labs(title = "20220503&#12398;&#27598;&#26178;&#12398;&#26368;&#39640;&#27671;&#28201;") +
  theme(title = element_text(colour = "#ffffe0"),
        axis.title = element_text(colour = "#ffffe0"),
        axis.text = element_text(colour = "#ffffe0"),
        strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill = "white"),
        axis.text.y = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid = element_blank(), 
        panel.background = element_rect(fill = "lightgray"),
        plot.background = element_rect(fill = "#0a0a0a"))

出力例

グラフはクリックで大きく見ることができます。


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

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