将sf shapefile数据帧与外部tibble连接会产生错误

时间:2018-02-17 14:28:53

标签: r geospatial shapefile sp sf

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

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()

结果:

enter image description here

但我的目标是将外部数据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列的名称和类在两个数据框中是相同的吗?

第二个问题,当我尝试仅绘制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()中读取原始形状文件之后直接绘制原始形状文件,并在调用库(sf)时说:Linking to GEOS 3.4.2, GDAL 2.1.2, proj.4 4.9.1

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

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

我认为你需要&#34;&#34;对于inner_join中的by参数。这就是dplyr进行连接操作的方式。 by参数需要标准评估,与其他dplyr函数不同,我们可以在那里使用非标准评估。