【R】日本国内の都道府県別人口分布を地図上に色分け表示

library(rvest)
library(leaflet)
library(dplyr)
library(magrittr)
library(stringr)
library(NipponMap)

kenmei_base = c("北海道", "青森", "岩手", "宮城", "秋田", "山形", "福島", "茨城",
           "栃木", "群馬", "埼玉", "千葉", "東京", "神奈川", "新潟", "富山",
           "石川", "福井", "山梨", "長野", "岐阜", "静岡", "愛知", "三重",
           "滋賀", "京都", "大阪", "兵庫", "奈良", "和歌山", "鳥取", "島根",
           "岡山", "広島", "山口", "徳島", "香川", "愛媛", "高知", "福岡",
           "佐賀", "長崎", "熊本", "大分", "宮崎", "鹿児島", "沖縄")

x<-read_html("https://en.wikipedia.org/wiki/List_of_Japanese_prefectures_by_area")
tab<-x %>% html_table(header = TRUE, fill=TRUE)
class(tab)

pilgr <- tab %>% extract2(1)

pilgr %>% glimpse()

popul <- pilgr %>% use_series(Population) %>% str_extract_all(pattern = "[1-9][0-9]\\,[0-9][0-9][0-9]\\,[0-9][0-9][0-9]+|[0-9]\\,[0-9][0-9][0-9]\\,[0-9][0-9][0-9]+|[0-9][0-9][0-9]\\,[0-9][0-9][0-9]", simplify = TRUE) %>% extract(2:48, 1) %>% gsub(",", "", .) %>% as_data_frame()  #%>%  mutate_if(is.character, as.numeric)  #set_colnames(c("number", "polul"))
kenmei <- pilgr %>% use_series(Japanese)
kenmei <- kenmei[c(2:48)] %>% gsub("県", "", .) %>% gsub("東京都", "東京", .) %>% gsub("府", "", .)

dt <- data.frame("Kenmei"=kenmei, "Population"=popul)

dat = rep("", 47)
names(cols) = kenmei
for(i in 1:47){
  for(j in 1:47){
    if(kenmei_base[i] == kenmei[j]){
      dat[i] <- dt$value[j]
    }
  }
}

dat<- as.integer(dat)
mec <- cut(dat, hist(dat, plot=FALSE)$breaks, right=FALSE)
mcol <- heat.colors(length(levels(mec)))[as.integer(mec)]

windowsFonts(JP1=windowsFont("MS Gothic"),
             JP2=windowsFont("MS Mincho"),
             JP3=windowsFont("Meiryo"),
             JP4=windowsFont("Biz Gothic"))
windows(width=800, height=800)
par(family="JP4")

JapanPrefMap(mcol, main="Population in Japanese prefecture")
legend("bottomright", fill=heat.colors(length(levels(mec))), legend=names(table(mec)))

Add a Comment

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