Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created November 25, 2014 07:46
Show Gist options
  • Save komasaru/6847442be52197ea6519 to your computer and use it in GitHub Desktop.
Save komasaru/6847442be52197ea6519 to your computer and use it in GitHub Desktop.
R scirpt to draw a map of Japan. (Okianwa Division Ver.)
# ----------------------------------------
# R scirpt to draw a map of Japan. (Okianwa Division Ver.)
# ----------------------------------------
# ライブラリの読み込み
library(gpclib)
library(ggplot2)
library(maptools)
# ----------------------------------------
print("**** 各種設定")
# グラフタイトル
TITLE <- "日本地図(沖縄移動 Ver.)"
# コピーライト文字列
STR_COPY <- paste(
"(地図出典:国土交通省 - 国土数値情報(行政区域データ))",
"© 2014 mk-mode.com",
sep = "\n"
)
# Shapefile のフルパス
FILE_SHP <- "/path/to/japan_all.shp"
# 保存ファイル
FILE_SAV <- "japan_okinawa.png"
# ----------------------------------------
print("**** Shapefile の読み込み")
shp <- readShapePoly(FILE_SHP, IDvar = "N03_007")
shp.hon <- subset(shp, substring(shp$N03_007, 1, 2) != "47") # 本土分抽出
shp.oki <- subset(shp, substring(shp$N03_007, 1, 2) == "47") # 沖縄分抽出
# ----------------------------------------
print("**** データフレーム形式に変換")
df.hon <- fortify(shp.hon) # 本土分
df.oki <- fortify(shp.oki) # 沖縄分
df.oki$long <- df.oki$long + 6 # 東へ 6度移動
df.oki$lat <- df.oki$lat + 17.5 # 北へ17.5度移動
# ----------------------------------------
print("**** 境界線")
border <- data.frame(
xx <- c(129, 132.5, 137.5, 137.5),
yy <- c( 40, 40, 42.5, 45)
) # 境界線
# ----------------------------------------
print("**** 日本地図描画")
g <- ggplot() # オブジェクト生成
g <- g + ggtitle(TITLE) # グラフタイトル
g <- g + geom_polygon(
data = df.hon,
aes(long, lat, group = group),
colour = "gray20", fill = "darkolivegreen3", size = 0.05
) # 本土描画
g <- g + geom_polygon(
data = df.oki,
aes(long, lat, group = group),
colour = "gray20", fill = "darkolivegreen3", size = 0.05
) # 沖縄描画
g <- g + geom_path(
data = border, aes(xx, yy),
colour = "gray40", size = 0.5
) # 境界線描画
g <- g + xlim(128, 150) + ylim(27, 46)
g <- g + labs(
x = STR_COPY, y = ""
)
g <- g + coord_equal() # メモリ刻み等間隔
g <- g + theme(legend.position = "none") # 凡例位置
g <- g + theme(
panel.background = element_rect(
colour = "black", fill = "lightsteelblue",
size = 0.2 , linetype = 1
)
) # グラフ枠・背景
# ----------------------------------------
print("**** 画像保存")
ggsave(
file = FILE_SAV,
dpi = 100, width = 8, height = 8,
g
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment