在choropleth图中处理引起NAs的缺失区域
我有下面的数据框,我想为它创建一个chorpleth映射。我从下载了germany shapefile,然后使用此代码创建地图。如您所见,地图已创建,但由于缺少几个区域,因此它们被设置为在choropleth图中处理引起NAs的缺失区域,r,dictionary,leaflet,choropleth,R,Dictionary,Leaflet,Choropleth,我有下面的数据框,我想为它创建一个chorpleth映射。我从下载了germany shapefile,然后使用此代码创建地图。如您所见,地图已创建,但由于缺少几个区域,因此它们被设置为NAs,并显示为黑色。我如何处理这个问题?可能会删除它们或将它们更改为0?如果他们能解决这个问题,我愿意接受其他的软件包,比如传单 region<-c("09366", "94130", "02627"
NAs
,并显示为黑色。我如何处理这个问题?可能会删除它们或将它们更改为0
?如果他们能解决这个问题,我愿意接受其他的软件包,比如传单
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()
regionPackagesf
将使您的流程更简单
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()
不错的方法这能与缩放功能互动吗?很好的选择。我想这可以嵌入一个闪亮的应用程序中
library(tmap)
tmap_mode("view")
tm_shape(shp = germany_sf) +
tm_polygons(col = "value", border.alpha = 0)