问题描述
我正在尝试使用ggplot
绘制世界地图.我的代码在我的要点文件中.当我不使用coord_map
时输出是正确的,但是当我使用coord_map
时非常奇怪:
I'm trying to draw a world map using ggplot
. My code is in my gist file. The output is correct when I don't use coord_map
but very strange when I use coord_map
:
ggplot(data = test, aes(fill = x)) +
geom_map(aes(map_id = id), map =world.ggmap, color = "white") +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
theme_tufte() +
coord_map()
的地图>
ggplot(data = test, aes(fill = x)) +
geom_map(aes(map_id = id), map =world.ggmap, color = "white") +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
theme_tufte()
当我使用maps包中的数据时,我遇到了相同的错误:
I've got the same error when I use data from the maps package :
library(ggplot2)
library(maps)
world <- map_data("world")
ggplot() +
geom_map( data=world, aes(x=long, y=lat, group = group, map_id = region),colour="white", fill="grey10", map = world ) +
coord_map()
有人回答吗?
推荐答案
由于经度值超出[-180,180]范围,因此我之前也遇到过类似的问题.在您的示例中,数据没有此问题,但我的技巧似乎在这里也有效.就我而言,我只是使用"xlim"来排除有问题的数据.
I had a similar problem before, due to longitude values outside the range [-180,180]. In your example the data do not have this problem but my trick seems to work also here. In my case I just used 'xlim' to exclude the problematic data.
此解决方案似乎也适用于您的情况(我使用了您的要旨中的代码):
This solution seems to work in your case also (I used the code from your gist):
map+coord_map(xlim=c(-180,180))
它会产生以下地图:
南极洲仍然存在问题,如果不需要此区域,您也可以考虑对其进行裁剪:
There is still a problem with Antarctica, you can also consider clipping it too if you don't need this area:
map+coord_map(xlim=c(-180,180), ylim=c(-60, 90))
这篇关于为什么coord_map产生奇怪的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!