Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created November 25, 2014 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/e8deebb95ccced8aaf99 to your computer and use it in GitHub Desktop.
Save komasaru/e8deebb95ccced8aaf99 to your computer and use it in GitHub Desktop.
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