在ggplot2中绘制大型多边形地图的小区域 [英] plot small region of a large polygon map in ggplot2

查看:343
本文介绍了在ggplot2中绘制大型多边形地图的小区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 geom_polygon 来加强和绘制ggplot2中的shapefile文件。我怎么能只绘制这张地图的一个小区域?



我的完整地图看起来不错,但是我的小地区很混乱。

下面是一个工作示例:
这个小形状文件可以从下面获得:


在坐标系上设置限制会缩小绘图(就像你在看它一样与放大镜),并不会改变基础数据,如设置限制的规模会。


在你的情况下,你有一个 map ,您可以使用 coord_map ,它会使用地图投影来投影您的数据。



例如

$ pre $ g $ p $ ggplot(spf1,aes(x = long,y = lat,group = group))+
geom_polygon(color ='grey90')+
coord_map(xlim = c(-2,2),ylim = c(5 0,51))


I have a shapefile which I fortified and plotted in ggplot2 using geom_polygon. How can I plot only a small region of this map?

My full map looks fine, but my small region is messed up.

Here is a working example: This small shapefile can be obtained from:

http://www.mappinghacks.com/data/TM_WORLD_BORDERS_SIMPL-0.2.zip

#read data
spf<-readOGR(getwd(),"TM_WORLD_BORDERS_SIMPL-0.2")
spf@data$id<-rownames(spf@data)

#fortify
spf1<-fortify(spf,region="id")

#full plot
ggplot(spf1)+geom_polygon(aes(long,lat,group=group),colour="grey90")

#subset plot #this is messy since polygons are broken
ggplot(spf1)+geom_polygon(aes(long,lat,group=group),colour="grey90")+
scale_x_continuous(limits = c(-2, 2))+
scale_y_continuous(limits = c(50, 51))

Thanks.

解决方案

The limits argument in scale_x_... and scale_y... sets the limits of the scale. Any values outside these limits are not drawn (the underlying data is dropped). This includes elements (such as a polygon) which may only be partially outside these limits.

If you want to zoom the plot in by setting the limits on the coordinates, then use the xlim and ylim arguments to a coord_.... function, From ?coord_cartesian

Setting limits on the coordinate system will zoom the plot (like you're looking at it with a magnifying glass), and will not change the underlying data like setting limits on a scale will.

In your case you have a map, and you can use coord_map, which will project your data using a map projection.

eg

ggplot(spf1, aes(x=long,y=lat,group=group)) + 
  geom_polygon(colour  = 'grey90') +
  coord_map(xlim = c(-2, 2),ylim = c(50, 51))

这篇关于在ggplot2中绘制大型多边形地图的小区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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