【问题标题】:change NA-color in zipcode map更改邮政编码地图中的 NAV 颜色
【发布时间】:2019-02-20 18:25:02
【问题描述】:

我用我的数据创建了一张德国邮政编码的地图。我的数据不包含每个邮政编码,所以我确实有缺失值。缺失值在地图中被绘制为黑色;我希望它们是白色的。怎么改?

ger_plz <- readOGR(dsn = ".", layer = "plz-2stellig")

gpclibPermit()
#convert the raw data to a data.frame as ggplot works on data.frames
ger_plz@data$id <- rownames(ger_plz@data)
ger_plz.point <- fortify(ger_plz, region="id")
ger_plz.df <- inner_join(ger_plz.point,ger_plz@data, by="id")

head(ger_plz.df)
ggplot(ger_plz.df, aes(long, lat, group=group )) + geom_polygon()

#data file
Ers <- table(data.plz$plz)
df<- as.data.frame(Ers)

# variable name 'region' is needed for choroplethr
ger_plz.df$region <- ger_plz.df$plz
head(ger_plz.df)

#subclass choroplethr to make a class for your my need
GERPLZChoropleth <- R6Class("GERPLZChoropleth",
                            inherit = choroplethr:::Choropleth,
                            public = list(
                              initialize = function(user.df) {
                                super$initialize(ger_plz.df, user.df)
                              }
                            )
)
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c("region", "value")

#instantiate new class with data
c <- GERPLZChoropleth$new(df)

#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Erstsemester Landau 2017"
c$legend= "Heimatort"
c$set_num_colors(9)
c$render()

【问题讨论】:

    标签: r colors zipcode


    【解决方案1】:

    我为您提供了一种解决方案,但这不是最好的。 您需要绘制地图,然后您可以更改颜色。选择另一个调色板以使 NA 数据变为白色。我用调色板=“OrRd”试了一下,效果很好。

    c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
    c$title = "Erstsemester Landau 2017"
    c$legend= "Heimatort"
    c$set_num_colors(9)
    choro <- c$render()
    choro
    choro+scale_fill_brewer(palette = "OrRd")
    

    然后您也可以将调色板参数与其他颜色一起使用:

    发散: BrBG、PiYG、PRGn、PuOr、RdBu、RdGy、RdYlBu、RdYlGn、光谱

    定性口音: Dark2、成对、Pastel1、Pastel2、Set1、Set2、Set3

    顺序: 蓝色,BuGn,BuPu,GnBu,绿色,灰色,橙色,OrRd,PuBu,PuBuGn,PuRd,紫色,RdPu,红色,YlGn,YlGnBu,YlOrBr,YlOrRd

    (来源:https://www.rdocumentation.org/packages/ggplot2/versions/3.3.2/topics/scale_colour_brewer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多