使用ggplot绘制带有孔的多边形(在城市地图中) [英] Use ggplot to plot polygon with holes (in a city map)

查看:162
本文介绍了使用ggplot绘制带有孔的多边形(在城市地图中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个用于创建城市地图的shp文件:


  • land.shp(绘制水面以上的地形的多边形)

  • road.shp(绘制所有道路的多边形,注意其中一些是圆形道路,这意味着一个洞位于中间)

  • building.shp(绘制所有建筑物的多边形)



我使用QGIS绘制我想要的地图,然后使用ggplot播放land.shp,然后road.shp和building shp再次执行。下面这个是从Google地图输出来说明我的问题:



你可以看到有两座桥和一些大海(我没有海水柱,我只是将背景设置为蓝色)之间,用蓝点标记。在R中,那个区域应该是一个洞,但它全是灰色的。同样的问题发送到使用红点标记的灰色区域,这是一块土地,另一个灰色区域使用绿点标记,这是一个被道路包围的建筑物。



<我会在road.shp的洞口有陆地/海洋/建筑物,我不能用R来展示它们。


任何人都可以教我如何显示R中的road.shp层背后的东西?感谢。

解决方案




  • 一个闭合的多边形,点的进度为逆时针,形成一个实体形状
  • 带有顺时针顺时针的多边形形成


数据和图:

  library(ggplot2)

id< - letters [1:2]

#用于填充颜色的ID和值
值< - data.frame(
id = ids,
value = c(4,5)


#多边形位置
位置< - data.frame(
id = rep(id,每个= 10),
#形状孔形状孔
x = c(1,4,4,1,1,2,2,3,3,2,5,10,10,5,5,6,6,9,9,6),
y = c(1,1,4,4,1,2,3,3,2,2,5,5,10,10,5,6,9,9,6,6)


#合并位置和值
datapoly< - merge(values,p通过= c(id))

ggplot(aes(x = x,y = y))+
geom_polygon(aes(group = id,fill = factor (value)))+
scale_fill_discrete(Key)


I have 3 shp files for create a city map:

  • land.shp (polygon drawing the land which is above water)
  • road.shp (polygon drawing all the roads, note that some of them are "circular road", which means a hole is in the middle)
  • building.shp (polygon drawing all buildings)

I used QGIS to plot the map I want, then I use ggplot to play the land.shp, then road.shp and building shp to do it again. The one below is output from Google map to illustrate my issue:

You can see there are 2 bridges and some sea (I don't have sea shp, I just set the background to be blue) between them, tagged using blue dot. In R, that area should be a hole, but it is all filled with grey. The same issue go to the grey area tagged using red dot, which is a piece of land, and another grey area tagged using green dot, which is a building surrounded by road.

I will have land/sea/building in the hole of road.shp, I can't show them using R.

Can anyone teach me how to show the things behind the road.shp layer in R? Thanks.

解决方案

One common convention to draw polygons with holes is:

  • A closed polygon with points that progress anti-clockwise forms a solid shape
  • A closed polygon with points progressing clockwise forms a hole

So, let's construct some data and plot:

library(ggplot2)

ids <- letters[1:2]

# IDs and values to use for fill colour
values <- data.frame(
  id = ids,
  value = c(4,5)
)

# Polygon position
positions <- data.frame(
  id = rep(ids, each = 10),
  #     shape      hole         shape        hole
  x = c(1,4,4,1,1, 2,2,3,3,2,   5,10,10,5,5, 6,6,9,9,6),
  y = c(1,1,4,4,1, 2,3,3,2,2,   5,5,10,10,5, 6,9,9,6,6)
)

# Merge positions and values
datapoly <- merge(values, positions, by=c("id"))

ggplot(datapoly, aes(x=x, y=y)) + 
  geom_polygon(aes(group=id, fill=factor(value))) +
  scale_fill_discrete("Key")

这篇关于使用ggplot绘制带有孔的多边形(在城市地图中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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