如何在R语言中覆盖全局地图 [英] How to overlay global map on filled contour in R language

查看:199
本文介绍了如何在R语言中覆盖全局地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些填充轮廓的lat-lon数据,然后在顶部覆盖地球的地图。我试图这样:

  filled.contour(lons,lats,glb.data,
plot.axes = {axis(1); axis(2); map(projection ='rectangular',parameters = 0,add = T)}

轮廓很好地绘制出来,并且地图显示出来,但只是在等值线图的一个小区域中非常小的黑色矩形。当然,我想让地图在整个填充轮廓图的整个区域上展开。



我也试过类似的东西:

  filled.contour (lons,lats,glb.data,
plot.axes = {axis(1); axis(2); map(projection ='rectangular',x = lons,y = lats,parameters = 0,add = T)}

在这种情况下,地图根本不绘制,得到一个错误信息没有东西可以画出来:所有地区都是越界
人们显然总是成功地做到这一点,但是我一直无法使其工作。谁能告诉我我做错了什么?顺便说一句,我不想​​使用格子包。它不会与我的团队正在做的其他事情一起工作。谢谢。

解决方案

好的。对于那些关心的人,我有一个答案。也许不是最好的答案,而是一个答案。问题是有两种常见的方式来表示经度和纬度:度和弧度。有第三种常用的表示经度的方法:本地时间,即时间区域时间,一般化为浮点数。然后,为了让生活更有趣,以度为单位的经度经常以-180(东)至180(西)或0至360来给出。

现在,当你尝试显示地图,R会根据地图包的内部自动选择显示的经度单位。如果你在一套单位工作,但R想要在另一套单位显示地图,你会遇到麻烦。另一个缺点是不同的地图投影想要以不同的单位显示。正如我所发现的,默认投影喜欢度数;它想显示在一个绘图矩形中,经度的范围为-180至180,纬度的范围为-90至90。但是,矩形投影虽然看起来非常像默认投影,却喜欢弧度;它想显示在一个矩形中,伸展到pi(经度)和-pi / 2到pi / 2(纬度)。

所以这里是你需要做的:
1)确定哪个地图投影,然后确定它被绘制的限制:

 > map(projection = foo,parameters = foo)
> par('usr')#将返回小区限制向量



<2>知道你自己的数据单位如果你刚刚抓住别人的数据集,你可能会感到惊讶。



<3>当你绘制...

  filled.contour(lons,lats,glb.data)

如果您的lons和lats的单位与投影所期望的范围一致,那么您应该可以,并且您应该可以简单地添加一张带有

  filled.contour(lons,lats,glb.data,
plot.axes = {axis(1,...); axis (2,...); map(projection = foo,parameters = foo,add = T)})



但是,如果你的投影和投影都不是投影​​所需要的,那么你就有更多的工作要做。您必须通过调用par()来重新定义剧情的极限。这是我的工作:

  filled.contour(lons,lats,glb.data,
plot.axes = {axis(1,...); axis(2,...); par(usr = c(-180,180,-90,90));
map(add = T)})

当我需要位于情节左端的主要子午线时(我经度处处增加我做了两个调用par()和map():

  filled.contour(lons ,lats,glb.data,
plot.axes = {axis(1,...); axis(2,...);
par(usr = c(0,360,-90,90 )); map(add = T); #eastern hemisphere
par(usr = c(-360,0,-90,90)); map(add = T)} #western

还有一些皱纹。 (例如'矩形'投影没有限制 -pi + pi)。但是,如果我迄今为止所说的话是有道理的,那么您将能够处理它们。

I would like to plot some lat-lon data in a filled contour, and then overlay a map of the globe on top. I am trying something like this:

filled.contour(lons, lats, glb.data,
plot.axes={axis(1);axis(2);map(projection='rectangular',parameters=0,add=T)}
)

The contour is plotted fine, and the map shows up, but only as a very tiny little black rectangle in a small area of the contour plot. Of course, I want the map to stretch over the entire area of the filled contour plot.

I've also tried something like:

filled.contour(lons, lats, glb.data,
plot.axes={axis(1);axis(2);map(projection='rectangular',x=lons,y=lats,parameters=0,add=T)}
)

In this case, the map does not plot at all and I get an error message "nothing to draw: all regions out of bounds" People apparently do this successfully all the time, but I haven't been able to get it to work. Can anyone tell me what I am doing wrong? BTW, I don't want to use the lattice package. It will not work with the other stuff my group is doing. Thanks.

解决方案

OK. for anyone who cares, I have an answer. Maybe not the best answer, but an answer. The problem is that there are 2 common ways to represent latitude and longitude: degrees and radians. There is a third common way to represent longitude: local time, i.e. time zone time generalized as floating point numbers. And then, to make life more interesting, longitude in degrees is often given as -180 (east) to 180 (West) OR 0 to 360.

Now, when you try to display a map, R automatically picks the lat-lon units for the display based on the innards of the maps package. If you are working in one set of units, but R wants to display the map in another set, you get trouble. Another wrinkle is that different map projections want to be displayed with different units. As I have discovered, the "default" projection likes degrees; it wants to be displayed in a plot rectangle with limits -180 to 180 in longitude, and -90 to 90 in latitude. BUT, the "rectangular" projection, though it looks very much like the default projection, likes radians; it wants to be displayed in a rectangle stretching -pi to pi (longitude) and -pi/2 to pi/2 (latitude). These plot limits are not discussed at all in ?mapproject.

So here's what you need to do: 1) Determine which map projection you want and then determine what limits it is plotted in:

> map(projection=foo,parameters=foo)
> par('usr')   # will return vector of plot limits

2) Know the units your own data is expressed in. If you have just grabbed someone else's data set, you might be surprised here.

3) When you plot ...

filled.contour(lons, lats, glb.data)

if your "lons" and "lats" are in units consistent with the limits your projection expects, then you should be OK, and you should be able to simply add a map with

filled.contour(lons, lats, glb.data,
plot.axes={axis(1,...);axis(2,...);map(projection=foo,parameters=foo,add=T)})

But if your lons and lats are not what the projection needs, then you have a little more work to do. You have to redefine the limits of your plot with a call to par(). This is what worked for me:

filled.contour(lons, lats, glb.data,
plot.axes={axis(1,...);axis(2,...);par(usr=c(-180,180,-90,90));
map(add=T)})

And when I needed the prime meridian at the left end of the plot (i,e, longitude everywhere increasing on the x axis from 0 to 360) I made two calls to par() and map():

filled.contour(lons, lats, glb.data,
plot.axes={axis(1,...);axis(2,...);
par(usr=c(0,360,-90,90));map(add=T);  #eastern hemisphere
par(usr=c(-360,0,-90,90));map(add=T)}   #western
)

There are some other wrinkles too. (e.g. the 'rectangular' projection does not have limits of precisely -pi an +pi.) But if what I have said so far makes sense, you will be able to deal with them.

这篇关于如何在R语言中覆盖全局地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆