Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#==========================
# 日本地図描画関数を定義
# JapanPlot これは2次元です
# 日本地図を描画する関数
# 都道府県をローマ字で指定するとその県のみ描画する(複数指定可能)
# ローマ字の対応については以下を参照のこと
PreNames <-
c("Aichi","Ehime","Ibaraki","Okayama","Okinawa","Iwate","Gifu","Miyazaki",
"Miyagi","Kyoto","Kumamoto","Gunma","Hiroshima", "Kagawa","Kochi","Saga",
"Saitama","Mie","Yamagata","Yamaguchi","Yamanashi","Shiga","Kagoshima","Akita",
"Niigata","Kanagawa","Aomori","Shizuoka","Ishikawa","Chiba","Osaka","Oita",
"Nagasaki","Nagano","Tottori","Shimane","Tokyo","Tokushima","Tochigi", "Nara",
"Toyama","Fukui","Fukuoka","Fukushima","Hyōgo","Hokkaido","Wakayama")
#==========================
#==========================
# 注意
# あらかじめmap.dataとmap.Allを作成しておくこと
#==========================
#==========================
# 引数説明
# preName : allのとき日本地図全体をプロット, ローマ字で頭文字を大文字の状態で都道府県を指定すると指定した県のみプロット
# 複数指定が可能
# jpn.xlim : プロットする際のx軸の範囲を指定, auto で自動で座標の最大値、最小値を考慮してプロットする
# jpn.ylim : jpn.xlimと同様
# 注意 : 東京など島が多い地域では主要部がかなり小さく出てしまうことがあるので、その場合はjpn.xlim, ylimを自分で
# 指定した値で実行できる
# 関数定義
JapanPlot <- function(preName='all', jpn.xlim='auto', jpn.ylim='auto'){
if(length(preName)==1){
# 日本全体を描く場合
if(preName=='all'){
# 範囲が自動なら以下のように定義する
if(length(jpn.xlim)==1&&jpn.xlim=='auto'){
jpn.xlim <- c(125, 150)
}
if(length(jpn.ylim==1)&&jpn.ylim=='auto'){
jpn.ylim <- c(25, 48)
}
#
n <- length(map.All)
for(i in 1:n){
tmp1 <- map.All[[i]]
if(i==1){
plot(tmp1[,1], tmp1[,2], xlim=jpn.xlim, ylim=jpn.ylim, type='l', xlab='', ylab='')
}else{
lines(tmp1[,1], tmp1[,2])
}
}
}else{
# 都道府県を1つだけプロットするとき
     n3 <- length(preName)
num2 <- which(names(map.data) %in% preName)
mymin <- function(x){ apply(x, 2, min) }
mymax <- function(x){ apply(x, 2, max) }
axisMin <- apply(t(as.data.frame(lapply(map.data[num2], function(X){lapply(X, mymin)}))), 2, min)
axisMax <- apply(t(as.data.frame(lapply(map.data[num2], function(X){lapply(X, mymax)}))), 2, max)
if(length(jpn.xlim)==1&&jpn.xlim=='auto'){
jpn.xlim <- c(axisMin[1], axisMax[1])
}
if(length(jpn.ylim==1)&&jpn.ylim=='auto'){
jpn.ylim <- c(axisMin[2], axisMax[2])
}
num1 <- which(names(map.data)==preName)
tmpData <- map.data[[num1]]
n2 <- length(tmpData)
for(i in 1:n2){
tmp1 <- tmpData[[i]]
if(i==1){
plot(tmp1[,1], tmp1[,2], xlim=jpn.xlim, ylim=jpn.ylim, type='l',
xlab='', ylab='')
}else{
lines(tmp1[,1], tmp1[,2])
}
}
}
}else{
# 都道府県を2つ以上プロットするとき
n3 <- length(preName)
num2 <- which(names(map.data) %in% preName)
mymin <- function(x){ apply(x, 2, min) }
mymax <- function(x){ apply(x, 2, max) }
axisMin <- apply(t(as.data.frame(lapply(map.data[num2], function(X){lapply(X, mymin)}))), 2, min)
axisMax <- apply(t(as.data.frame(lapply(map.data[num2], function(X){lapply(X, mymax)}))), 2, max)
if(length(jpn.xlim)==1&&jpn.xlim=='auto'){
jpn.xlim <- c(axisMin[1], axisMax[1])
}
if(length(jpn.ylim==1)&&jpn.ylim=='auto'){
jpn.ylim <- c(axisMin[2], axisMax[2])
}
for(i in 1:n3){
num1 <- which(names(map.data)==preName[i])
tmpData <- map.data[[num1]]
n4 <- length(tmpData)
for(j in 1:n4){
tmp1 <- tmpData[[j]]
if(i==1){
plot(tmp1[,1], tmp1[,2], xlim=jpn.xlim, ylim=jpn.ylim, xlab='', ylab='', type='l')
}else{
lines(tmp1[,1], tmp1[,2])
}
}
}
}
}
#==========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment