我正在使用R maps包在世界地图上绘制一些点,例如:

绘制基本地图的命令是:
map("world", fill=TRUE, col="white", bg="gray", ylim=c(-60, 90), mar=c(0,0,0,0))
Run Code Online (Sandbox Code Playgroud)
但我需要显示太平洋中心地图.我使用map("world2",etc来使用maps包中的太平洋中心底图,并将我的dataframe(df)中数据点的坐标转换为:
df$longitude[df$longitude < 0] = df$longitude[df$longitude < 0] + 360
Run Code Online (Sandbox Code Playgroud)
如果我不使用该fill选项,但是使用fill交叉0°的多边形会导致问题.

我想我需要以maps某种方式从库中转换多边形数据来对其进行排序,但我不知道如何解决这个问题.
我理想的解决方案是绘制一个左边界为-20°,右边界为-30°(即330°)的地图.以下内容将正确的点和海岸线放到地图上,但是交叉零问题是相同的
df$longitude[df$longitude < -20] = df$longitude[d$longitude < -20] + 360
map("world", fill=TRUE, col="white", bg="gray", mar=c(0,0,0,0),
ylim=c(-60, 90), xlim=c(-20, 330))
map("world2", add=TRUE, col="white", bg="gray", fill=TRUE, xlim=c(180, 330))
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Jor*_*eys 17
您可以使用内部事实,可以重新计算函数map返回的对象并在map()函数中再次使用map().我将创建一个包含单个多边形的列表,检查哪些具有非常不同的经度值,然后重新排列这些多边形.我在下面的函数*中给出了这种方法的一个例子,它允许类似于:
plot.map("world", center=180, col="white",bg="gray",
fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))
Run Code Online (Sandbox Code Playgroud)
要得到

如果我是你,我会更多地改变一切,比如:
plot.map("world", center=200, col="white",bg="gray",
fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))
Run Code Online (Sandbox Code Playgroud)

功能 :
plot.map<- function(database,center,...){
Obj <- map(database,...,plot=F)
coord <- cbind(Obj[[1]],Obj[[2]])
# split up the coordinates
id <- rle(!is.na(coord[,1]))
id <- matrix(c(1,cumsum(id$lengths)),ncol=2,byrow=T)
polygons <- apply(id,1,function(i){coord[i[1]:i[2],]})
# split up polygons that differ too much
polygons <- lapply(polygons,function(x){
x[,1] <- x[,1] + center
x[,1] <- ifelse(x[,1]>180,x[,1]-360,x[,1])
if(sum(diff(x[,1])>300,na.rm=T) >0){
id <- x[,1] < 0
x <- rbind(x[id,],c(NA,NA),x[!id,])
}
x
})
# reconstruct the object
polygons <- do.call(rbind,polygons)
Obj[[1]] <- polygons[,1]
Obj[[2]] <- polygons[,2]
map(Obj,...)
}
Run Code Online (Sandbox Code Playgroud)
*请注意,此功能仅采用正中心值.它很容易适应两个方向的中心值,但我不再烦恼,因为这是微不足道的.
安装最新版本的地图(3.2.0)。
做这个:
d$lon2 <- ifelse(d$lon < -25, d$lon + 360, d$lon) # where d is your df
mapWorld <- map_data('world', wrap=c(-25,335), ylim=c(-55,75))
ggplot() +
geom_polygon(data = mapWorld, aes(x=long, y = lat, group = group)) +
geom_point(data = d, aes(x = lon2, y = lat))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10997 次 |
| 最近记录: |