[英]Fixing maps library data for Pacific centred (0°-360° longitude) display
I'm plotting some points on a map of the world using the R maps
package, something like: 我正在使用R
maps
包在世界地图上绘制一些点,例如:
The command to draw the base map is: 绘制基本地图的命令是:
map("world", fill=TRUE, col="white", bg="gray", ylim=c(-60, 90), mar=c(0,0,0,0))
But I need to display Pacific centred map. 但我需要显示太平洋中心地图。 I use
map("world2",
etc to use the Pacific centred basemap from the maps package, and convert the coordinates of the data points in my dataframe ( df
) with: 我使用
map("world2",
等来使用来自maps包的太平洋中心底图,并将我的数据框( df
)中的数据点的坐标转换为:
df$longitude[df$longitude < 0] = df$longitude[df$longitude < 0] + 360
This works if I don't use the fill
option, but with fill
the polygons which cross 0° cause problems. 如果我不使用
fill
选项,但是fill
0°的多边形会导致问题,这是有效的。
I guess I need to transform the polygon data from the maps
library somehow to sort this out, but I have no idea how to get at this. 我想我需要从转换多边形数据
maps
库以某种方式排序了这一点,但我不知道怎么弄这个。
My ideal solution would be to draw a maps with a left boundary at -20° and a right boundary at -30° (ie 330°). 我理想的解决方案是绘制一个左边界为-20°,右边界为-30°(即330°)的地图。 The following gets the correct points and coastlines onto the map, but the crossing-zero problem is the same
以下内容将正确的点和海岸线放到地图上,但是交叉零问题是相同的
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))
Any help would be greatly appreciated. 任何帮助将不胜感激。
You could use the fact that internally, a map
object returned by the map()
function can be recalculated and used again in the map()
function. 您可以使用内部事实,
map()
函数返回的map
对象可以重新计算并在map()
函数中再次使用。 I'd create a list with individual polygons, check which ones have very different longitude values, and rearrange those ones. 我将创建一个包含单个多边形的列表,检查哪些具有非常不同的经度值,然后重新排列这些多边形。 I gave an example of this approach in the function below*, which allows something like :
我在下面的函数*中给出了这种方法的一个例子,它允许类似于:
plot.map("world", center=180, col="white",bg="gray",
fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))
to get 要得到
If I were you, I'd shift everything a bit more, like in : 如果我是你,我会更多地改变一切,比如:
plot.map("world", center=200, col="white",bg="gray",
fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))
The function : 功能 :
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,...)
}
*Note that this function only takes positive center values. *请注意,此功能仅采用正中心值。 It's easily adapted to allow for center values in both directions, but I didn't bother anymore as that's trivial.
它很容易适应两个方向的中心值,但我不再烦恼,因为这是微不足道的。
install the latest version of maps (3.2.0). 安装最新版本的地图(3.2.0)。
do this: 做这个:
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))
A bit late, but you can also create a shifted map by using a projection (requires the mapproj package): 有点晚了,但你也可以使用投影创建一个移位的地图(需要mapproj包):
map("world", projection="rectangular", parameter=0,
orientation=c(90,0,180), wrap=TRUE, fill=T, resolution=0,col=0)
This will shift by 180 degrees. 这将移动180度。 But the difference with 'world2' is that the longitude co-ordinate will be different ([-pi,pi]).
但与'world2'的不同之处在于经度坐标会有所不同([-pi,pi])。 All projections of this package put 0 at the centre.
该套餐的所有预测均以0为中心。 And in that case, the 'wrap' option detects the jump correctly.
在这种情况下,'wrap'选项可以正确检测跳转。
'resolution=0' helps to get cleaner borders. 'resolution = 0'有助于获得更清晰的边界。
You can easily change the centre longitude by changing the '180' value in the projection description. 您可以通过更改投影描述中的“180”值轻松更改中心经度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.