我只是想用白色填充简单多边形之外的区域。出于某种原因,它在中心画了一根奇怪的木桩,就像它认为这是一个吸血鬼杀手或其他什么东西一样,搞砸了。
我尝试跟随这个帖子 https://stackoverflow.com/questions/21286643/ggplot2-gis-plotting-inside-polygon-area但有些事情已经变得疯狂了。我本以为这会更容易,但事实证明它是一个相当暴躁的小恶魔。
如何在不破坏该区域的情况下对投影友好的多边形外部区域进行白色填充inside多边形?谢谢
# reproducible example
library(rgeos)
library(maptools)
shpct.tf <- tempfile() ; td <- tempdir()
download.file(
"ftp://ftp2.census.gov/geo/pvs/tiger2010st/09_Connecticut/09/tl_2010_09_state10.zip" ,
shpct.tf ,
mode = 'wb'
)
shpct.uz <- unzip( shpct.tf , exdir = td )
# read in connecticut
ct.shp <- readShapePoly( shpct.uz[ grep( 'shp$' , shpct.uz ) ] )
# box outside of connecticut
ct.shp.env <- gEnvelope( ct.shp )
# difference between connecticut and its box
ct.shp.diff <- gDifference( ct.shp.env , ct.shp )
# prepare both shapes for ggplot2
f.ct.shp <- fortify( ct.shp )
outside <- fortify( ct.shp.diff )
library(ggplot2)
# create all layers + projections
plot <- ggplot(data = f.ct.shp, aes(x = long, y = lat)) #start with the base-plot
layer1 <- geom_polygon(data=f.ct.shp, aes(x=long,y=lat), fill='black')
layer2 <- geom_polygon(data=outside, aes(x=long,y=lat), fill='white')
co <- coord_map( project = "merc" )
# this works
plot + layer1
# this does not
plot + layer1 + layer2
# this also does not
plot + layer1 + layer2 + co
ct.shp.diff
由四个多边形组成:
R> length(ct.shp.diff@polygons[[1]]@Polygons)
# 4
or
R> nlevels(outside$group)
# 4
因此,你需要一种群体审美layer2
(否则 ggplot 会尝试绘制单个多边形,这会导致各部分之间出现奇怪的连接):
layer2 <- geom_polygon(data=outside, aes(x=long, y=lat, group=group), fill='white')
plot + layer1 + layer2 + co
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)