Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
R scirpt to draw a map of Japan. (Standard Ver.)
# ----------------------------------------
# R scirpt to draw a map of Japan. (Standard 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_standard.png"
# ----------------------------------------
print("**** Shapefile の読み込み")
shp <- readShapePoly(FILE_SHP, IDvar = "N03_007")
# ----------------------------------------
print("**** データフレーム形式に変換")
df <- fortify(shp)
# ----------------------------------------
print("**** 日本地図描画")
g <- ggplot() # オブジェクト生成
g <- g + ggtitle(TITLE) # グラフタイトル
g <- g + geom_polygon(
data = df,
aes(long, lat, group = group),
colour = "gray20", fill = "darkolivegreen3", size = 0.05
) # 地図描画
g <- g + xlim(124, 150) + ylim(24, 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
You can’t perform that action at this time.