简体   繁体   English

R:Choropleth map 与 ggplot2(连接属性)

[英]R: Choropleth map with ggplot2 (join attributes)

I was trying to create a choropleth map using ggplot2 and a public shape-file of Switzerland (it is included in this zip-file : PLZO_PLZ.shp)我试图使用 ggplot2 和瑞士的公共形状文件(它包含在这个 zip 文件中:PLZO_PLZ.shp)

library(rgdal)    
library(ggplot2)   
library(ggmap)   
library(broom)

# Load Shapefile
shapefile <- readOGR(file.choose())

# Next the shapefile has to be converted to a dataframe for use in ggplot2
shapefile_df <- fortify(shapefile)

ggplot(data = shapefile_df) +
  geom_path(aes(x = long, y = lat, group = group),  color="black") +
  theme_void() 

The result looks like this:结果如下所示:

在此处输入图像描述

However, I don't want to plot the map only, I also want to color the different municipalities according to certain attributes.但是,我不想只 plot map,我还想根据某些属性为不同的城市着色。 Each municipality has a four-digit zip code (called "PLZ").每个自治市都有一个四位数的 zip 代码(称为“PLZ”)。 These values can be seen in shapefile$PLZ .这些值可以在shapefile$PLZ中看到。

But how can I assign attributes to certain municipalities?但是我怎样才能将属性分配给某些城市呢? An example:一个例子:

fakeData <- data.frame(PLZ = c(8001, 8002, 8048), values = c(20, 40, 99))

I would like to colour the municipalities 8001, 8002 and 8048. But how can I join the variable values into shapefile or shapefile_df ?我想为市镇 8001、8002 和 8048 着色。但是如何将变量values加入shapefileshapefile_df So that I can plot it like this:这样我就可以像这样 plot :

ggplot(data = shapefile_df) +
  geom_path(aes(x = long, y = lat, group = group, fill = values),  color="black") +
  theme_void() 

If I try "left_join" on shapefile the error message is that the method is not applicable to an object of the class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial')".如果我在shapefile上尝试“left_join”,则错误消息是该方法不适用于 class“c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial')”的 object。 In shapefile_df however, there is no variable PLZ any more.然而,在shapefile_df中,不再有变量PLZ I have also tried我也试过

shapefile@data$id <- rownames(shapefile@data)
shapefile_df <- fortify(shapefile, region = "PLZ")

which leads to the following error: Error in maptools::unionSpatialPolygons(cp, attr[, region]): isTRUE(gpclibPermitStatus()) is not TRUE这导致以下错误: Error in maptools::unionSpatialPolygons(cp, attr[, region]): isTRUE(gpclibPermitStatus()) is not TRUE

I would recommend using the sf package to read the shapefile.我建议使用sf package 来读取 shapefile。 Then you can do the left_join without any problems.然后你可以毫无问题地做left_join Finally, you can do the plot using geom_sf and change the fill default values for NA from gray to transparent using scale_fill_distiller (you can also choose the fill palette for non-NA values).最后,您可以使用scale_fill_distiller执行 plot 并使用geom_sf将 NA 的填充默认值从灰色更改为透明(您也可以为非 NA 值选择填充调色板)。

library(sf)

# Read shapefile as sf
shapefile_df <- st_read(file.choose())

# Left_join by PLZ
shapefile_df <- shapefile_df %>%
  left_join(fakeData, by = "PLZ")

# Do plot
ggplot(shapefile_df, 
       aes(fill = values)) + 
  # Plot sf 
  geom_sf(show.legend = F) +
  # Indicate fill palette and set NA values to transparent
  scale_fill_distiller(type = "seq",
                       palette = "Blues",
                       na.value = "transparent") +
  # Add void theme
  theme_void()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM