Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active August 29, 2015 14:04
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/c58347f2b78f1825ba86 to your computer and use it in GitHub Desktop.
Save komasaru/c58347f2b78f1825ba86 to your computer and use it in GitHub Desktop.
R script to generate a map of KSJ by maptools.
# ライブラリの読み込み
library(gpclib)
library(ggplot2)
library(maptools)
# gpclib ライセンス警告表示の抑止
gpclibPermit()
# Shape ファイルのフルパス
shp_file <- '/path/to/N03-140401_32_GML/N03-14_32_140401.shp'
# Shape ファイルの読み込み
shp <- readShapePoly(shp_file)
# 「松江市(32201)」分のみ抽出
shp.matsue <- subset(shp, shp$N03_007 == "32201")
# データフレーム形式に変換
df <- fortify(shp)
df.matsue <- fortify(shp.matsue)
# Plot
g <- ggplot(df) # オブジェクト生成
g <- g + ggtitle("島根県松江市") # グラフタイトル
g <- g + geom_polygon(
aes(long, lat, group = group),
colour = "gray20", size = 0.1,
fill = "ivory2") # 島根県全体
g <- g + geom_polygon(
data = df.matsue,
aes(long, lat, group = group),
colour = "gray20", size = 0.5,
fill = "cornsilk4") # 松江市のみを重ねあわせ
g <- g + coord_equal() # メモリ刻み等間隔
g <- g + theme(legend.position = "none") # 凡例位置
g <- g + theme(
panel.background = element_rect(
colour = "black", fill = "lavender",
size= 0.2 , linetype = 1
)
) # グラフ枠・背景
#print(g) # 画像表示
# 画像保存
ggsave(
file = "test_maptools_ksj.png",
dpi = 100, width = 6.4, height = 6.4,
g
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment