我一直在使用一组网格单元的shapefile来制作没有问题的地图。最近,我得到了一个略微不同的网格单元shapefile,并要求将两者合并并重新生成相同的地图。现在,我已经加入了两个SF对象,现在无法绘制地图了。
我不确定如何为此创建一个reprex。 shapefile本身可以在ftp://ftp.dfg.ca.gov/R7_MR/MANAGEMENT/
中以caltrawl.zip
和CRFS/MAN_CA_CRFS_microblocks2013.zip
的形式免费获得。
这是原始代码,可以很好地绘制出来(这只是为了总结“微块”以达到感兴趣的分辨率,从而创建一个更大或更小的对象):
microblocks <- read_sf("CRFS_microblocks/MAN_CA_CRFS_microblocks2013.shp")
blocks1 <- microblocks %>%
st_buffer(0) %>% # magical function that prevents errors when combining polygons below
group_by(BLOCK10_ID) %>% # so it doesn't just lump into one giant polygon ...
mutate(
Xmean = mean(X),
Ymean = mean(Y)
) %>%
ungroup() %>%
group_by(BLOCK10_ID, Xmean, Ymean) %>%
summarise() %>% # should yield a sf object
ungroup() %>%
st_transform(crs="+proj=longlat +datum=NAD83")
并且ggplot(blocks1) + geom_sf()
正常工作。
但是,当我尝试加入第二个几何caltrawl
...
caltrawl <- read_sf("caltrawl/caltrawl.shp") %>%
dplyr::select(BLOCK10_ID) %>%
blocks2 <- microblocks %>%
st_buffer(0) %>% # magical function that prevents errors when combining polygons below
group_by(BLOCK10_ID) %>% # so it doesn't just lump into one giant polygon ...
mutate(
Xmean = mean(X),
Ymean = mean(Y)
) %>%
ungroup() %>%
group_by(BLOCK10_ID, Xmean, Ymean) %>%
summarise() %>% # should yield a sf object
ungroup() %>%
dplyr::select(-Xmean, -Ymean) %>% # since we're adding more blocks
as.data.frame() %>% # necessary to de-sf-ify to join the other blocks
full_join(caltrawl, by="BLOCK10_ID") %>% # full join retains blocks from both
st_sf(sf_column_name = 'geometry.x') %>%
st_transform(crs="+proj=longlat +datum=NAD83") as.data.frame()
我得到一个对象,其绘制得很好,为plot(blocks2)
,但是当我尝试ggplot(blocks2) + geom_sf()
时,出现以下错误:
Don't know how to automatically pick scale for object of type standardGeneric. Defaulting to continuous.
Error: Column `geometry` must be a 1d atomic vector or a list
还值得注意的是,它们都是SF对象,但是类有所不同:
> class(blocks1)
[1] "sf" "tbl_df" "tbl" "data.frame"
> class(blocks2)
[1] "sf" "data.frame"
在blocks2
情况下,我在做什么错?