Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#==========================
# 日本地図描画関数を定義
# JapanPlot3d
# 日本地図を描画する関数
# 都道府県をローマ字で指定するとその県のみ描画する(複数指定可能)
# ローマ字の対応については以下を参照のこと
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を作成しておくこと
#==========================
#==========================
# パッケージ読み込み
library(rgl)
#==========================
#==========================
# 関数定義
# 基本的に2Dのときと同じです
JapanPlot3d <- function(preName='all', jpn.xlim='auto', jpn.ylim='auto', jpn.zlim='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)
}
if(length(jpn.zlim)==1 && jpn.zlim=='auto'){
jpn.zlim <- c(0,200)
}
n <- length(map.All)
for(i in 1:n){
tmp1 <- map.All[[i]]
if(i==1){
plot3d(tmp1[,1], tmp1[,2], 0, xlim=jpn.xlim, ylim=jpn.ylim, zlim=jpn.zlim,
axes=FALSE, xlab='', ylab='', zlab='')
}else{
lines3d(tmp1[,1], tmp1[,2], 0)
}
}
}else{
num1 <- which(names(map.data)==preName)
tmpData <- map.data[[num1]]
n2 <- length(tmpData)
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(125, 150)
}else{
jpn.xlim <- c(axisMin[1], axisMax[1])
}
if(length(jpn.ylim)==1 && jpn.ylim=='auto'){
jpn.ylim <- c(25, 48)
}else{
jpn.ylim <- c(axisMin[2], axisMax[2])
}
if(length(jpn.zlim)==1 && jpn.zlim=='auto'){
jpn.zlim <- c(0,200)
}
for(i in 1:n2){
tmp1 <- tmpData[[i]]
if(i==1){
plot3d(tmp1[,1], tmp1[,2], 0, xlim=jpn.xlim, ylim=jpn.ylim, zlim=jpn.zlim,
axes=FALSE, xlab='', ylab='', zlab='')
}else{
lines3d(tmp1[,1], tmp1[,2], 0)
}
}
}
}else{
n3 <- length(preName)
for(i in 1:n3){
num1 <- which(names(map.data)==preName[i])
tmpData <- map.data[[num1]]
n4 <- length(tmpData)
mymin <- function(x){ apply(x, 2, min) }
mymax <- function(x){ apply(x, 2, max) }
axisMin <- apply(t(as.data.frame(lapply(map.data[num1], function(X){lapply(X, mymin)}))), 2, min)
axisMax <- apply(t(as.data.frame(lapply(map.data[num1], function(X){lapply(X, mymax)}))), 2, max)
if(length(jpn.xlim)==1 && jpn.xlim=='auto'){
jpn.xlim <- c(125, 150)
}else{
jpn.xlim <- c(axisMin[1], axisMax[1])
}
if(length(jpn.ylim)==1 && jpn.ylim=='auto'){
jpn.ylim <- c(25, 48)
}else{
jpn.ylim <- c(axisMin[2], axisMax[2])
}
if(length(jpn.zlim)==1 && jpn.zlim=='auto'){
jpn.zlim <- c(0,200)
}
for(j in 1:n4){
tmp1 <- tmpData[[j]]
if(i==1){
plot3d(tmp1[,1], tmp1[,2], 0, xlim=jpn.xlim, ylim=jpn.ylim, zlim=jpn.zlim,
axes=FALSE, xlab='', ylab='', zlab='', type='l')
}else{
lines3d(tmp1[,1], tmp1[,2], 0)
}
}
}
}
}
#==========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment