【发布时间】: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()
【问题讨论】: