将 sf shapefile 数据框与外部 tibble 连接会出现错误

Joining sf shapefile data frame with external tibble gives errors

我正在尝试使用以下形状文件在荷兰地图上绘制多边形数据。

library(sf)
library(tidyverse)

download.file("http://www.imergis.nl/shp/2018-Imergis_gemeentegrenzen_kustlijn-shp.zip", "shapefile.zip")
unzip("shapefile.zip")

shp <- st_read("2018-Imergis_gemeentegrenzen_kustlijn.shp")

现在,如果我直接绘制简单的要素集合就可以了,下面绘制了所有城市。

shp %>%
  ggplot() +
  geom_sf() +
  theme_minimal()

结果:

但我的目标是将外部数据 example_df 连接到 shp 数据框。这是我的小example_df

example_df <- structure(list(Gemeente = c("Amsterdam", "Rotterdam"), Beleidscode = c("Vervaardigen harddrugs", 
"Vervaardigen harddrugs"), inwoners_2017 = c(844947L, 634660L
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L), vars = c("Gemeente", "Beleidscode"), drop = TRUE, indices = list(
    0L, 1L), group_sizes = c(1L, 1L), biggest_group_size = 1L, labels = structure(list(
    Gemeente = c("Amsterdam", "Rotterdam"), Beleidscode = c("Vervaardigen harddrugs", 
    "Vervaardigen harddrugs")), class = "data.frame", row.names = c(NA, 
-2L), vars = c("Gemeente", "Beleidscode"), drop = TRUE, .Names = c("Gemeente", 
"Beleidscode")), .Names = c("Gemeente", "Beleidscode", "inwoners_2017"
))


# Convert and rename so that this column is the same example_df
shp <- shp %>% rename(Gemeente = gemeentena) %>%
  mutate(Gemeente = as.character(Gemeente))

shp_joined <- inner_join(shp, example_df, by = Gemeente)

第一个问题:Error in common_by(by, x, y) : object 'Gemeente' not found怎么来的? Gemeente 列的名称和 class 在两个数据框中是否相同?

第二个问题,当我尝试只绘制 shp 现在它是 data.frame 时出现以下错误:

Error in if (st_is_longlat(crs)) bb = trim_bb(bb, margin) : 
  missing value where TRUE/FALSE needed

我还要说的是,当我安装 sf 软件包时,出现了这些警告:

==================================================
downloaded 7.1 MB

* installing *source* package ‘sf’ ...
** package ‘sf’ successfully unpacked and MD5 sums checked
configure: CC: clang
configure: CXX: clang++
checking for gdal-config... /usr/local/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 1.11.5
checking GDAL version >= 2.0.0... no
configure: error: sf is not compatible with GDAL versions below 2.0.0
ERROR: configuration failed for package ‘sf’
* removing ‘/Users/Thomas/Library/R/3.3/library/sf’
Warning in install.packages :
  installation of package ‘sf’ had non-zero exit status

The downloaded source packages are in ...

但它确实在 st_read() 中读取原始形状文件后直接绘制它,并且在调用 library(sf) 时它说:Linking to GEOS 3.4.2, GDAL 2.1.2, proj.4 4.9.1

希望有人能提供帮助。

请尝试shp_joined <- inner_join(shp, example_df, by = "Gemeente")

我认为 inner_join 中的 by 参数需要 ""。这就是 dplyr 进行连接操作的方式。 by 参数需要标准评估,不像其他 dplyr 函数,我们可以在那里使用 non-standard 评估。