Subsetting data and graphing on a world map using sf in R: "Error: Object not found"

Subsetting data and graphing on a world map using sf in R: "Error: Object not found"

我正在使用 RspData 包中的 worldworldbank_df 数据集,我需要对我的数据进行子集化并绘制结果图表。我正在选择“大陆”和“urban_pop”列,删除所有 NA 值,按大陆分组,并汇总所有大陆的平均城市人口。但是,当我使用 geom_sf 调用绘制结果图表时,出现错误:

Error in FUN(X[[i]], ...) : object 'mean_urban_pop' not found

我需要在世界地图中绘制此数据,但它对我不起作用,因为 geom 坐标未传输到我的新数据集。

如何将这些结果绘制成图表?

注意:下面投影的坐标系是我必须使用的。

这是我的代码:

library(tidyverse)
library(sf)
library(spData)
library(ggplot2)

#Load in the data
world <- spData::world
wdb <- spData::worldbank_df

#Combine the data frames
wld_jn <- left_join(world, wdb, by = c('iso_a2', 'name_long' = 'name'))

#Reproject to required coordinate system. Obtained from: https://spatialreference.org/ref/sr-org/6/
wld_jn <- st_transform(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

def_wld_jn <- st_set_crs(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

#Graph mean urban population across continent:
wld_jn %>%
  select(continent, urban_pop) %>% 
  drop_na() %>% 
  group_by(continent) %>% 
  summarise_at(vars(urban_pop), list(mean_urban_pop = mean)) %>% 
  ggplot(.) +
  geom_sf(data = wld_jn, aes(fill = mean_urban_pop)) +
  scale_fill_gradient2(midpoint = 285)+
  guides(fill = guide_colorbar(title = "Population",
                             title.position = "bottom",
                             title.theme = element_text(size = 10,
                                                        face = "bold",
                                                        colour = "gray70",
                                                        angle = 0))) +
  ggtitle("World Urban Population") +
  theme(plot.title = element_text(hjust = 0.5))

看起来问题出在使用 summarise() 而不是 mutate() 上;当您使用 summarise() 时,您只保留感兴趣的变量,例如

library(tidyverse)
head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mtcars %>%
  group_by(cyl) %>%
  summarise(mean_mpg = mean(mpg)) %>%
  head()
#> # A tibble: 3 × 2
#>     cyl mean_mpg
#>   <dbl>    <dbl>
#> 1     4     26.7
#> 2     6     19.7
#> 3     8     15.1

mtcars %>%
  group_by(cyl) %>%
  mutate(mean_mpg = mean(mpg)) %>%
  head()
#> # A tibble: 6 × 12
#> # Groups:   cyl [3]
#>     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb mean_mpg
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl>
#> 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4     19.7
#> 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4     19.7
#> 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1     26.7
#> 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1     19.7
#> 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2     15.1
#> 6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1     19.7

reprex package (v2.0.1)

于 2022-02-23 创建

如果您将 sumamrise() 更改为 mutate()(并将 geometry = geom 添加到 geom_sf()),则不会出现错误。这能解决您的问题吗?

library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
#install.packages("spData")
library(spData)
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`

#Load in the data
world <- spData::world
wdb <- spData::worldbank_df

#Combine the data frames
wld_jn <- left_join(world, wdb, by = c('iso_a2', 'name_long' = 'name'))

#Reproject to required coordinate system. Obtained from: https://spatialreference.org/ref/sr-org/6/
wld_jn <- st_transform(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')

def_wld_jn <- st_set_crs(wld_jn, '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs')


#Graph mean urban population across continent:
wld_jn %>%
  select(continent, urban_pop) %>% 
  drop_na() %>%
  group_by(continent) %>% 
  mutate(mean_urban_pop = mean(urban_pop)) %>% 
  ggplot() +
  geom_sf(aes(fill = mean_urban_pop, geometry = geom)) +
  scale_fill_gradient2(midpoint = 285)+
  guides(fill = guide_colorbar(title = "Population",
                               title.position = "bottom",
                               title.theme = element_text(size = 10,
                                                          face = "bold",
                                                          colour = "gray70",
                                                          angle = 0))) +
  ggtitle("World Urban Population") +
  theme(plot.title = element_text(hjust = 0.5))

reprex package (v2.0.1)

于 2022-02-23 创建

这就是您要找的吗?稍微采用了代码并且 看这里https://github.com/tidyverse/ggplot2/issues/3391

wld_jn %>%
  select(continent, urban_pop) %>% 
  drop_na() %>% 
  group_by(continent) %>% 
  summarise(mean_urban_pop = mean(urban_pop)) %>% 
  ggplot(.) +
  geom_sf(data = wld_jn, aes(fill = wld_jn$mean_urban_pop)) +
  scale_fill_gradient2(midpoint = 285)+
  guides(fill = guide_colorbar(title = "Population",
                               title.position = "bottom",
                               title.theme = element_text(size = 10,
                                                          face = "bold",
                                                          colour = "gray70",
                                                          angle = 0))) +
  ggtitle("World Urban Population") +
  theme(plot.title = element_text(hjust = 0.5))