Ggplot skips polygonal holes

I find it hard to get ggplot for building polygons with holes. The following is shown. First get shapefile with git clone https://github.com/geotheory/volcano. Further:

require(ggplot2); require(ggmap); require(dplyr); require(maptools)
v = readShapePoly('volcano/volcano.shp')
v@proj4string = CRS('+proj=longlat +datum=WGS84')

# confirm polygons spatially exclusive (don't overlap)
plot(t(bbox(v)), type='l', lwd=8)
plot(v, col=paste0(colorRampPalette(c('grey','red'))(8),'dd'), add=T)

enter image description here

Looks nice. A ddalpha should display an invisible line if it is hidden by multiple polygons. Now try in ggplot.

d = fortify(v) %>% as_data_frame()
bb = bbox(v)
toner = get_stamenmap(c(bb[1,1], bb[2,1], bb[1,2], bb[2,2]), zoom=11, maptype='toner')
ggmap(toner) + geom_polygon(data=d, aes(long, lat, group=group, fill=id), alpha=.5)

enter image description here

The center polygons must overlap because the basemap is completely hidden in the center. Let me check the reinforced data for the holes:

d %>% select(id, hole) %>% table()
   hole
id  FALSE TRUE
  0   278    0
  1   715    0
  2   392  388
  3   388  331
  4   390  265
  5   265  387
  6   328  125
  7   125    0

It looks good, so try to visualize them individually.

i = 3
plot(v[i,], col='red') 
ggplot(filter(d, id == i-1)) + geom_polygon(aes(long, lat, group=group, col=hole), fill=NA)
ggplot() + geom_polygon(data=filter(d, id==i-1), aes(long,lat, group=group))

enter image description here

Something seems wrong. Ggplot seems to ignore holes. If this is not a problem with the shapefile. Any suggestions on how to diagnose / fix this?

+4
2

"hasley/ggplot2: plotting polygon shapefiles" : "... , ggplot2". , ggspatial package " : ggplot" , , , , ( , "Biggin Hill" ggspatial, ggplot(d) + geom_polygon(aes(long, lat, group = group, fill = id)) env)

devtools::install_github("paleolimbot/ggspatial")

library(ggspatial)

ggmap(toner) + geom_spatial(data=v, aes(fill=id), alpha=.8)

enter image description here

+3

. ggplot2 , ( , , ).

, , .

colorsbrewer_red <-  c( "#fff5f0","#fee0d2", "#fcbba1","#fc9272", "#fb6a4a", "#ef3b2c", "#cb181d",  "#99000d")

ggmap(toner) +  
geom_polygon(data=d[!d$hole,], aes(long, lat, group=group, fill=id), alpha=.5) +
  scale_fill_manual(name = "",
                    values= colorsbrewer_red,
                    na.value = "# 808080",
                    drop=FALSE)

volcano_in_london

, - , geom_polygon, .

+1

Source: https://habr.com/ru/post/1658606/


All Articles