【R】群馬県の地図をggplotにて描画

群馬県の地図をシェープファイルからggplotを使って描画します。こちらを参考にしています。

library(ggplot2)
library(ggrepel)
library(sf)
library(dplyr)

map <- read_sf("shp/h27ka10.shp") # 群馬県のシェープファイル

ggplot(map) + geom_sf()

map %>%
  group_by(CITY_NAME) %>% # 市名でグルーピング
  summarize('Pop.total' = sum(JINKO))  %>% # グループ単位で領域結合, 人口計算
  mutate( # 重心計算と座標値の抽出
    centroid = st_centroid(geometry),
    x = st_coordinates(centroid)[, 1],
    y = st_coordinates(centroid)[, 2]
  ) %>%
  ggplot() +
  geom_sf(aes(fill = Pop.total)) +  # 人口毎に色分け
  coord_sf(datum = NA) + 
  scale_fill_viridis_c() +
  theme_void()+ 
  geom_text_repel(aes(x = x, y = y, label = CITY_NAME), col="orange",
                  family = "JP4", size = 4)

はい、こんな風にできます。

Add a Comment

メールアドレスが公開されることはありません。