SpatialPolygonDataFrame使用ggplot进行绘图 [英] SpatialPolygonDataFrame plotting using ggplot

查看:273
本文介绍了SpatialPolygonDataFrame使用ggplot进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个伦敦大区的形状文件。我使用 maptools 包中的 readShapePoly 函数将它作为 SpatialPolygonDataFrame



我想要绘制这些多边形。我已经通过使用 plot 中的函数
输出如图所示:


现在,我正在使用绘制相同的形状文件, ggplot2 但它不适用于我。
我得到了一些古怪的线条,如图所示:



我使用的代码是:

  london.wards< ;  -  readShapePoly(〜/ TD / london_wards2013 / london_wards2013.shp
,proj4string = CRS(projString))
wards.count< - nrow(london.wards@data)
#为每个lsoa分配id

london.wards@data$id< - 1:wards.count
wards.fort< - fortify(london.wards,region ='id')
ggplot(wards.fort,aes(long,lat))+ geom_polygon(color ='black',fill ='white')

其中projString是描述用于输入形状文件的投影的投影字符串。

解决方案

您需要添加额外的审美元素, group 。假设多边形ID称为 ID ,则synatx将如下所示:

  ggplot(wards.fort,aes(x = long,y = lat,group = ID))+ 
geom_polygon(color ='black',fill ='white')


I am having a shape file for the greater London area. I use the readShapePoly function from the maptools package to load it in R as a SpatialPolygonDataFrame .

I want to the plot those polygons .. Which I have already done by using the basic of plot function in R. The output looks as shown in this image:

Now, I am trying to plot the same shape file using ggplot2 but it doesn't work with me. I am getting some weird lines in the graph as shown blew:

The code I used was :

london.wards <- readShapePoly("~/TD/london_wards2013/london_wards2013.shp"
                          , proj4string=CRS(projString))
wards.count <- nrow(london.wards@data)
# assign id for each lsoa

london.wards@data$id <- 1:wards.count
wards.fort <- fortify(london.wards, region='id')
ggplot(wards.fort, aes(long, lat)) + geom_polygon(colour='black', fill='white')

where projString is the projection string describing the projection used for the input shape file.

解决方案

You need to add an additional aesthetic, group. Assuming the polygon id is called ID, the synatx will look like:

ggplot(wards.fort, aes(x = long, y = lat, group = ID)) + 
   geom_polygon(colour='black', fill='white')

这篇关于SpatialPolygonDataFrame使用ggplot进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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