Choropleth map in ggplot with polygons that have holes

后端 未结 4 540
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 05:17

I\'m trying to draw a choropleth map of Germany showing poverty rate by state (inspired by this question).

The problem is that some of the states (Berlin, for examp

4条回答
  •  甜味超标
    2020-11-30 05:51

    This is just an expansion on @Ista's answer, which does not require that one knows which states (Berlin, Bremen) need to be rendered last.

    This approach takes advantage of the fact that fortify(...) generates a column, hole which identifies whether a group of coordinates are a hole. So this renders all regions (id's) with any holes before (e.g. underneath) the regions without holes.

    Many thanks to @Ista, without whose answer I could not have come up with this (believe me, I spent many hours trying...)

    ggplot(map.df, aes(x=long, y=lat, group=group)) +
      geom_polygon(data=map.df[map.df$id %in% map.df[map.df$hole,]$id,],aes(fill=poverty))+
      geom_polygon(data=map.df[!map.df$id %in% map.df[map.df$hole,]$id,],aes(fill=poverty))+
      geom_path(colour="grey50")+
      scale_fill_gradientn(colours=brewer.pal(5,"OrRd"))+
      labs(x="",y="")+ theme_bw()+
      coord_fixed()
    

提交回复
热议问题