Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created July 27, 2014 01:29
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/5fb0db30ef3ccfc28a53 to your computer and use it in GitHub Desktop.
Save komasaru/5fb0db30ef3ccfc28a53 to your computer and use it in GitHub Desktop.
R script to plot earthquake infos on a map.
# 国土地理院・地球地図日本に地震情報をプロット
# ライブラリの読み込み
library(gpclib)
library(ggplot2)
library(maptools)
# gpclib ライセンス警告表示の抑止
gpclibPermit()
# Shapefile, CSV ファイルのフルパス
shp_file <- "/path/to/polbnda_jpn_pref.shp"
csv_file <- "/path/to/eq_data.csv"
# Shapefile の読み込み
shp <- readShapePoly(shp_file)
# Shapefile をデータフレーム形式に変換
df <- fortify(shp)
# CSV データ読み込み
csv <- read.csv(csv_file)
# タイトル・コピーライト文字列
title <- "地震の活動状況 [ 2013/01/01 - 2014/06/30 ]"
copy <- paste(
"(出典:国土地理院・地球地図日本 / 気象庁・防災情報XML)",
"© 2014 mk-mode.com", sep = "\n"
)
# 地図描画
g <- ggplot(df) # オブジェクト生成
g <- g + ggtitle(title) # グラフタイトル設定
g <- g + geom_polygon(
aes(long, lat, group = group),
colour = "gray20", fill = "darkolivegreen4", size = 0.1
) # 日本地図描画
g <- g + geom_point(
data = csv, shape = 1, alpha = 0.5,
aes(x = lon, y = lat, size = magnitude, colour = depth)
) # 地震情報 Plot
g <- g + scale_colour_gradient(low = "red", high="midnightblue") # 深さ色設定
g <- g + xlim(c(122, 150)) + ylim(c(23, 47)) # プロット範囲指定
g <- g + coord_equal() # メモリ刻みを等間隔設定
g <- g + theme(
plot.background = element_rect(
colour = "black", size = 0.5
)
) # プロット領域背景
g <- g + labs(
x = copy, y = "", size = "規模(M)", colour = "深さ(km)"
) # ラベル設定
g <- g + theme(legend.position = "right") # 凡例位置
g <- g + theme(
title = element_text(size = 10, colour = "black"), # タイトルのサイズ・色
axis.title.y = element_blank(), # y 軸ラベル領域非表示
axis.title.x = element_text(size = 10, colour = "gray20") # x 軸ラベルのサイズ・色
)
g <- g + guides(
colour = guide_legend(order = 1),
size = guide_legend(order = 2)
)
g <- g + theme(
panel.background = element_rect(
fill = "lightsteelblue", colour = "black",
size= 0.2 , linetype = 1
)
) # グラフ枠・背景
# 画像保存
ggsave(
file = paste("earthquakes.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