2013-08-19 77 views
7

我有一个shapefile,我使用geom_polygon在ggplot2中进行了加固和绘制。我怎么能只绘制这张地图的一个小区域?在ggplot2中绘制一个大型多边形地图的小区域

我的完整地图看起来不错,但我的小地区搞砸了。

这里是一个工作示例: 这个小shape文件可以从获得:

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

#read data 
spf<-readOGR(getwd(),"TM_WORLD_BORDERS_SIMPL-0.2") 
[email protected]$id<-rownames([email protected]) 

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

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

fullplot

#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)) 

enter image description here

感谢。

+1

来吧。正在使用哪些软件包。请? –

+0

哦,对不起。忘了宣布图书馆。 'require(rgdal)' 'require(ggplot2)' – rmf

回答

13

scale_x_...scale_y...中的参数limits设置了比例限制。这些限制之外的任何值都不会被绘制(底层数据将被删除)。这包括可能仅部分超出这些限制的元素(例如多边形)。

如果你想变焦剧情中通过坐标设定的极限,然后使用xlimylim参数为coord_....功能,从?coord_cartesian

设置限制的坐标系将放大情节(就像你用放大镜看的那样),并且不会改变基础数据,比如在一个刻度上设置限制。

你的情况,你有一个map,您可以使用coord_map,这将使用地图投影投影手机的数据。

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

enter image description here

+2

另外一点需要注意的是,'coord_map'需要坐标作为经纬度,或者需要指定一个特定的投影。对于UTM坐标,最好使用'coord_cartesian'。 – rmf

相关问题