LoginSignup
1

More than 3 years have passed since last update.

R言語でSOUND VOLTEX設置店舗数の塗り分け地図を作る

Last updated at Posted at 2019-06-13

ふと唐突に SOUND VOLTEX の設置店舗の分布ってどうなっているのか気になってしまったので、暇つぶしで調べてみました。

内容的には「R言語で日本地図を用いた塗り分け地図を作る記事」だと思ってもらって問題ないです。あくまでネタがSOUND VOLTEXってだけです。

作りたいもの

  • SOUND VOLTEXの各都道府県ごとの設置店舗数分布がどうなっているのか知りたい
  • せっかくなのでRで日本地図形式の塗り分け地図を作りたい

データ収集

SOUND VOLTEX の設置店舗は ココ で調べる事が出来ます。prefというパラメータに数値を投げれば各都道府県ごとに検索が出来るので、このヒット件数から都道府県別の店舗数を取得する事が出来ます。

mapget.R
library("rvest")
library("dplyr")
library("curl")

vec_pref <- c(1:47)
vec_num <- c()
vec_name <- c()

for (pref in vec_pref){
  # URL
  BaseURL <- "https://p.eagate.573.jp/game/sdvx/v/p/search/list.html"
  URL <- paste0(BaseURL, "?pref=", pref)

  # HTML
  soup <- read_html(URL)

  name <- soup %>% html_nodes(xpath = "//*[@id='main_center_cnt']/div[1]/form/div[1]/p[1]") %>% 
                   html_text() %>% substring(., 17, nchar(.) - 3)

  result <- soup %>% html_nodes(xpath = "//*[@id='main_center_cnt']/div[1]/div[4]") %>% html_text()
  shopnum <- as.integer(substring(result, 7, nchar(result) - 3))

  vec_name <- c(vec_name, name)
  vec_num <- c(vec_num, shopnum)

  Sys.sleep(1000)
}

data <- data.frame(vec_pref, vec_name, vec_num) 
colnames(data) <- c("pref", "name", "shopnum")

write.csv(data,"sdvxpref.csv")

スクレイピングは rvest でやってます。

日本地図にplot

いよいよ本題。
今回は このサイト を参考にして、塗り分け日本地図を作ります。

sdvxmap.R
library(RCurl)

eval(parse(text = getURL("https://raw.githubusercontent.com/fusion0202/RStat/master/map/map.R", ssl.verifypeer = FALSE)))
eval(parse(text = getURL("https://raw.githubusercontent.com/fusion0202/RStat/master/map/color_map3.R", ssl.verifypeer = FALSE)))

data <- read.csv("sdvxmap.csv")
t <- sort(data$shopnum)[-47]
rcol <- rev(heat.colors(47))

png("sdvxmap.png", width = 1000, height = 1000)
color.map3(data$shopnum, t, rcol)
dev.off() 
  • 参考元同様、群馬大学のShigenobu AOKI先生のこのスクリプトを利用してます
  • 実行時には作業ディレクトリ上に jpn.zip を展開しておいてください(DLリンクは参考元サイトにあります)
  • カラーパレットはrainbowではなくheat.colorsを使用。店舗数が多いほど赤くしたいので、rcolは反転しています。
  • getURLは、引用元サイトで書かれているURLをそのまま使うと失敗するので注意(現在はraw.github.comではなくraw.githubusercontent.comに置かれている)

完成した図

sdvxmap.png

別ver

せっかくなので、ヒートマップではなく閾値で7段階に分けてカラフルに塗り分けたverも作成してみました(凡例付き)

sdvxmap2.R
library(RCurl)

eval(parse(text = getURL("https://raw.githubusercontent.com/fusion0202/RStat/master/map/map.R", ssl.verifypeer = FALSE)))
eval(parse(text = getURL("https://raw.githubusercontent.com/fusion0202/RStat/master/map/color_map3.R", ssl.verifypeer = FALSE)))

data <- read.csv("sdvxmap.csv")
t <- c(5, 10, 20, 30, 40, 100)
rcol <- c("white", "paleturquoise", "palegreen", "yellow", "gold", "indianred1", "red")

png("sdvxmap2.png", width = 1000, height = 1000)
color.map3(data$shopnum, t, rcol)
legend("topleft", 
       legend=c("~5", "5~10", "10~20", "20~30", "30~40", "40~100", "100~"), 
       fill=rcol, cex=2)
dev.off() 

完成した図

sdvxmap2.png

こっちの方が組み分け地図っぽい図になってるかもしれない。適当に色を決めたのでセンスが無いのはご愛敬

あとがき

西日本って意外と全域的に設置店舗数が少ないんですね...
京都ですら15店しか無いのは驚きでした。

これだけだとあれなので、また気が向いたら筐体数での分布図や、RShinyを使った図でも作った記事を書きたいと思います。気が向いたらですが。

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
1