更改邮政编码地图中的NA颜色

时间:2018-09-16 18:51:40

标签: r colors zipcode

我使用数据在德国邮政编码上创建了地图。我的数据没有包含每个邮政编码,因此确实缺少值。缺少的值在地图上绘制为黑色;我希望他们是白人。我该如何更改?

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

1 个答案:

答案 0 :(得分:0)

我为您提供了一种解决方案,但这不是最好的。 您需要绘制地图,然后可以更改颜色。选择另一个调色板以使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