处理在 choropleth 地图中引起 NA 的缺失区域
Deal with missing regions which provoke NAs in choropleth map
我有下面的数据框,我想为其创建一个 chorpleth 地图。我从 here 下载了德国 shapefile,然后使用此代码创建了地图。如您所见,地图已创建,但因为我缺少几个区域,它们被设置为 NAs
并且它们变成黑色。我该如何处理这个问题?也许消除它们或将它们更改为 0
?我对其他软件包开放,例如 leaflet
或其他可以解决问题的软件包。
region<-c("09366",
"94130",
"02627",
"95336",
"08525",
"92637",
"95138",
"74177",
"08606",
"94152" )
value<-c( 39.5,
519.,
5.67,
5.10,
5.08,
1165,
342,
775,
3532,
61.1 )
df<-data.frame(region,value)
#shapefile from http://www.suche-postleitzahl.org/downloads?download=zuordnung_plz_ort.csv
library(choroplethr)
library(dplyr)
library(ggplot2)
library(rgdal)
library(maptools)
library(gpclib)
library(readr)
library(R6)
ger_plz <- readOGR(dsn = ".", layer = "plz-gebiete")
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
#df <- produce_sunburst_sequences
# 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)
}
)
)
#df<-df[,c(6,13)]
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c("region", "value")
#df<-df[!(df$region=="Missing_company_zip"),]
#df<-df[!duplicated(df$region), ]
#instantiate new class with data
c <- GERPLZChoropleth$new(df)
#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Comparison of number of Inhabitants per Zipcode in Germany"
c$legend= "Number of Inhabitants per Zipcode"
c$set_num_colors(9)
c$render()
包 sf
将使您的过程更容易。
library(tidyverse)
library(sf)
df <- data.frame(region = c("09366", "94130", "02627", "95336", "08525", "92637", "95138", "74177", "08606", "94152"),
value = c(39.5, 519, 5.67, 5.1, 5.08, 1165, 342, 775, 3532, 61.1))
germany_sf <- sf::st_read(dsn = "plz-gebiete.shp") %>%
left_join(df, by = c("plz" = "region"))
germany_sf %>%
ggplot() +
geom_sf(alpha = 0.1, size = 0.1, colour = "gray") +
geom_sf(data = . %>% filter(!is.na(value)), aes(fill = value)) +
scale_fill_viridis_c() +
theme_bw()
对于 zoomable/interactive 选项,请使用 {tmap}
,一个包装 {leaflet}
的包,用于快速、简单的地图。
library(tmap)
tmap_mode("view")
tm_shape(shp = germany_sf) +
tm_polygons(col = "value", border.alpha = 0)
我一直在摆弄 choroplethr 包,遇到了同样的问题。 “啊哈”的时刻是了解到各种 x_choropleth 函数的输出实际上只是一个 ggplot 对象。这意味着您可以像修改任何 ggplot 图形一样修改它们。因此,如果您在图形输出管道中添加类似这样的内容,我认为它可能会实现您所追求的目标:
+ scale_fill_distiller(na.value = "white")
不确定你在这里做的其他一些事情是否会阻止它工作。
我有下面的数据框,我想为其创建一个 chorpleth 地图。我从 here 下载了德国 shapefile,然后使用此代码创建了地图。如您所见,地图已创建,但因为我缺少几个区域,它们被设置为 NAs
并且它们变成黑色。我该如何处理这个问题?也许消除它们或将它们更改为 0
?我对其他软件包开放,例如 leaflet
或其他可以解决问题的软件包。
region<-c("09366",
"94130",
"02627",
"95336",
"08525",
"92637",
"95138",
"74177",
"08606",
"94152" )
value<-c( 39.5,
519.,
5.67,
5.10,
5.08,
1165,
342,
775,
3532,
61.1 )
df<-data.frame(region,value)
#shapefile from http://www.suche-postleitzahl.org/downloads?download=zuordnung_plz_ort.csv
library(choroplethr)
library(dplyr)
library(ggplot2)
library(rgdal)
library(maptools)
library(gpclib)
library(readr)
library(R6)
ger_plz <- readOGR(dsn = ".", layer = "plz-gebiete")
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
#df <- produce_sunburst_sequences
# 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)
}
)
)
#df<-df[,c(6,13)]
#choropleth needs these two columnames - 'region' and 'value'
colnames(df) = c("region", "value")
#df<-df[!(df$region=="Missing_company_zip"),]
#df<-df[!duplicated(df$region), ]
#instantiate new class with data
c <- GERPLZChoropleth$new(df)
#plot the data
c$ggplot_polygon = geom_polygon(aes(fill = value), color = NA)
c$title = "Comparison of number of Inhabitants per Zipcode in Germany"
c$legend= "Number of Inhabitants per Zipcode"
c$set_num_colors(9)
c$render()
包 sf
将使您的过程更容易。
library(tidyverse)
library(sf)
df <- data.frame(region = c("09366", "94130", "02627", "95336", "08525", "92637", "95138", "74177", "08606", "94152"),
value = c(39.5, 519, 5.67, 5.1, 5.08, 1165, 342, 775, 3532, 61.1))
germany_sf <- sf::st_read(dsn = "plz-gebiete.shp") %>%
left_join(df, by = c("plz" = "region"))
germany_sf %>%
ggplot() +
geom_sf(alpha = 0.1, size = 0.1, colour = "gray") +
geom_sf(data = . %>% filter(!is.na(value)), aes(fill = value)) +
scale_fill_viridis_c() +
theme_bw()
对于 zoomable/interactive 选项,请使用 {tmap}
,一个包装 {leaflet}
的包,用于快速、简单的地图。
library(tmap)
tmap_mode("view")
tm_shape(shp = germany_sf) +
tm_polygons(col = "value", border.alpha = 0)
我一直在摆弄 choroplethr 包,遇到了同样的问题。 “啊哈”的时刻是了解到各种 x_choropleth 函数的输出实际上只是一个 ggplot 对象。这意味着您可以像修改任何 ggplot 图形一样修改它们。因此,如果您在图形输出管道中添加类似这样的内容,我认为它可能会实现您所追求的目标:
+ scale_fill_distiller(na.value = "white")
不确定你在这里做的其他一些事情是否会阻止它工作。