r - 在邮政编码 map 中更改 NA 颜色

标签 r colors zipcode

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

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()

picture of my map

最佳答案

我为您提供了一种解决方案,但这不是最好的。 您需要绘制 map ,然后您可以更改颜色。选择另一个调色板以获得白色的 NA 数据。我用 palette = "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、光谱

定性口音: 深色 2、配对、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)

关于r - 在邮政编码 map 中更改 NA 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52357391/

相关文章:

r - 如何在列表中重叠到列表的子元素

r - 如何使用 dplyr 在 R 中以 "cascading"为基础计算一个值

r - 将二进制向量转换为二进制矩阵

java - 尝试划分颜色来绘制彩虹 donut

language-agnostic - 8位颜色可以通过平均混合吗?

linux - 为什么 super 简单的功能在我的 Mac 终端中无法显示 256 色?

r - 由于 rstan 编译问题,travis 构建失败

javascript - 显示特定州错误的邮政编码验证

mysql - 查找邮政编码指定距离内的所有邮政编码