简体   繁体   English

使用geom_polygon绘制带孔的ggplot多边形

[英]Plot ggplot polygons with holes with geom_polygon

There are questions out there about the fact that ggplot2 can't plot polygon shapes that have holes . 有些问题ggplot2无法绘制有孔的多边形形状。

That is because, if the order of points is not OK, the end graph looks bad, usually with clipping/trimming lines inside the donut shape. 这是因为,如果点的顺序不正确,则结束图看起来很糟糕,通常在圆环形状内部具有剪切/修剪线。

I have read a lot about how order matters, but I am not able to step forward. 我已经阅读了很多关于订单如何重要的内容,但我无法前进。 I have a SpatialPolygonsDataFrame with 26 features (comes from raster::rasterToPolygons(dissolve=T) ) and I want to plot it with ggplot . 我有一个具有26个功能的SpatialPolygonsDataFrame (来自raster::rasterToPolygons(dissolve=T) ),我想用ggplot绘制它。

Here's what happens - 这是发生了什么 -

r3.pol <- rasterToPolygons(r3, dissolve=T)
r3.df <- fortify(r3.pol)
names(r3.df) <- c('x','y','order','hole','piece','ID','group')
p <- ggplot(r3.df)
p <- p + geom_polygon(mapping=aes(x=x,y=y,group=ID), fill='red')
p <- p + coord_equal()

I see this output: 我看到这个输出:

在此输入图像描述

While it should be like so, with plot(r3.pol) : 虽然它应该是这样的,使用plot(r3.pol)

在此输入图像描述

How can I make this work? 我怎样才能做到这一点? I tried for hours but I am not able to reorder r3.df . 我试了几个小时,但我无法重新排序r3.df Also, can the information in r3.df$hole be helpful? 另外, r3.df$hole的信息可以帮助吗? It is returned by the function fortify for points that are holes (I think). 它是由函数fortify返回的,这些点是孔(我认为)。

Side question: how can I give you my r3.pol SpatialPolygonsDataFrame, so that you can try yourself? 附带问题:我怎样才能给你我的r3.pol SpatialPolygonsDataFrame,以便你可以尝试自己? I remember seeing long, reproducible "dumps" of objects here, but I don't know how to do it. 我记得在这里看到过长而可重复的“转储”对象,但我不知道该怎么做。

I save d the polygons data frame here . 在这里 save了多边形数据框。 Was not able to save it using dput , sorry. 抱歉,无法使用dput保存它。 You can fetch it using load . 您可以使用load获取它。

I suggest to install the package "ggpolypath" and use geom_polypath instead of geom_polygon. 我建议安装包“ggpolypath”并使用geom_polypath而不是geom_polygon。 Works for me. 适合我。

My temporary solution is: @#$% polygons, and use the raster package. 我的临时解决方案是:@#$%polygons,并使用raster包。

Namely: 即:

r <- raster(x=extent(r3.pol), crs=crs(r3.pol)) # empty raster from r3.pol
res(r) <- 250 # set a decent resolution (depends on your extent)
r <- setValues(r, 1) # fill r with ones
r <- mask(r, r3.pol) # clip r with the shape polygons

And now plot it as you would do with any raster with ggplot . 现在可以像使用ggplot任何raster一样绘制它。 The rasterVis package might come helpful here, but I'm not using it, so: rasterVis包在这里可能会有用,但我没有使用它,所以:

rdf <- data.frame(rasterToPoints(r))
p <- ggplot(rdf) + geom_raster(mapping=aes(x=x, y=y), fill='red')
p <- p + coord_equal()

And here it goes. 它就在这里。

在此输入图像描述

Alternatively, you can create the raster with rasterize , so the raster will hold the polygons values (in my case, just an integer): 或者,您可以使用栅格rasterize创建栅格,因此栅格将保存多边形值(在我的示例中,只是一个整数):

r <- raster(x=extent(r3.pol), crs=crs(r3.pol))
res(r) <- 250
r <- rasterize(r3.pol, r)

rdf <- data.frame(rasterToPoints(r))
p <- ggplot(rdf) + geom_raster(mapping=aes(x=x, y=y, fill=factor(layer)))
p <- p + coord_equal()

在此输入图像描述

If someone comes up with a decent solution for geom_polygon , probably involving re-ordering of the polygons data frame, I'll be glad to consider it. 如果有人为geom_polygon提出了一个不错的解决方案,可能涉及重新排序多边形数据框,我会很高兴考虑它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM