0

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

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列类相同?

第二个问题,当我现在尝试只绘制 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

希望有人可以提供帮助。

4

1 回答 1

1

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

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

于 2018-02-17T15:19:48.417 回答