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

Advertisements

I am working with the world and worldbank_df datasets in the spData package in R, and am needing to subset my data and graph my results. I am selecting the columns "continents" and "urban_pop," dropping all NA values, grouping by continent, and summarizing the mean urban population of all continents. However, when I go to graph the results using the geom_sf call, I am getting an error:

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

I need to graph this data in a world map, but it’s not working for me because the geom coordinates are not being transferred over to my new dataset.

How can I get these results graphed?

Note: The below coordinate system projected is the one that I must use.

Here is my code:

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

>Solution :

It looks like the issue is with the use of summarise() instead of mutate(); when you use summarise() you only keep the variable of interest, e.g.

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

Created on 2022-02-23 by the reprex package (v2.0.1)


If you change sumamrise() to mutate() (and add geometry = geom to geom_sf()) you don’t get the error. Does this solve your problem?

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

Created on 2022-02-23 by the reprex package (v2.0.1)

Leave a Reply Cancel reply