【问题标题】:Chloropleth: Control polygon fillsChoropleth:控制多边形填充
【发布时间】:2017-12-15 20:39:55
【问题描述】:

我已经上传了一个 shapefile here

#First, read it in
library(rgdal)
pols <- readOGR(dsn="stack", layer="shapefile")

plot.df <- broom::tidy(pols, by="nr") %>% mutate(id = as.numeric(id))

#some values I'd like to plot
plot.df$value[plot.df$id==0] <- 4
plot.df$value[plot.df$id==1] <- 3
plot.df$value[plot.df$id==2] <- 3

library(ggplot2)
ggplot(plot.df, aes(x=long, y=lat))+
    geom_polygon(aes(fill=factor(value), group=group))+
    coord_fixed()+
    scale_fill_manual(values = c("red", "green", "blue", "orange"),
                breaks = c("1", "2", "3", "4"))

此代码应生成以下图:

问题是,我有另一个应该出现的多边形(绿色)。

我想出了一个解决方法:

ggplot(plot.df, aes(x=long, y=lat))+
    geom_polygon(aes(fill=factor(value), group=group))+
    geom_polygon(data=subset(plot.df, id==0), aes(fill=factor(value), group=group))+
    coord_fixed()+ 
    scale_fill_manual(values = c("red", "green", "blue", "orange"),
                breaks = c("1", "2", "3", "4"))

但是,我确信有一个更优雅的解决方案。使用id 作为提议的here 的分组变量会导致多边形变形。

还有人有其他想法吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    多边形重叠,可以设置alpha

    ggplot(plot.df, aes(x = long, y = lat)) +
      geom_polygon(aes(fill = factor(value), group = group), alpha = .4)+
      coord_fixed() +
      scale_fill_manual(values = c("red", "green", "blue", "orange"), breaks = c("1", "2", "3", "4")) +
      theme(legend.position = 'none')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      相关资源
      最近更新 更多