Error in geom_sf(data = world, show.legend = F) : object '...' not found

Mara, I'm having exactly the same error pattern as stote did, but unfortunately no amount of re-installing, re-loading, or re-starting the session altogether is resolving it. My code was working beautifully yesterday, and then when I went to re-run today, I get the errors. So it must be an order or packages issue? SO grateful if you can help.

centroids <- read_delim("C:/Users/ch111111/Dropbox /Folder/centroids.csv", delim = "\t")%>%
mutate(country.code = countrycode(SHORT_NAME, "country.name","iso3c", warn = T))%>%
select(lat = "LAT", long = "LONG", country.code)

centroids[, c("lat", "long")] <- project(xy = as.matrix(centroids[, c("long", "lat")]), proj = "+proj=robin")

absolute_GDP <- read_excel(path, sheet = "Cumulative GDP lost (13)") %>%
rename(country = Country, cum_GDP = Cumulative GDP Lost ) %>%
mutate(iso = countrycode(country, "country.name", "iso3c")) %>%
mutate(cum_GDP = cum_GDP / 10^9)

centroids <- left_join(centroids, absolute_GDP, by = c("country.code" = "iso")) %>%
filter(!is.na(cum_GDP))

ggplot() +
geom_sf(data = world, show.legend = F) +
geom_point(data = centroids, aes(x = lat, y = long, size = cum_GDP)) +
scale_size_area(max_size = 25, breaks = c(0.1, 1, 50, floor(max(centroids$cum_GDP)))) +
theme_bw()

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

There's also a nice FAQ on how to do a minimal reprex for beginners, below:

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

1 Like

Mara, thanks for this! Here is my reprex:

library(datapasta)
library(ggplot2)
#datapasta:: df_paste(head(centroids, 5))

df <- data.frame(stringsAsFactors=FALSE,
                                                    lat = c(12396114.3738155, 1111619.42307639, 325672.675245503,
                                                            -7235015.91810587,
                                                            -6432333.51408773),
                                                   long = c(-2670804.78590195, 5043993.78855193, 5406895.92108048,
                                                            6328945.06430578,
                                                            -3204964.2454797),
                                           country.code = c("AUS", "AUT", "BEL", "CAN", "CHL"),
                                                country = c("Australia", "Austria", "Belgium", "Canada", "Chile"),
                                                cum_GDP = c(0.998576308865479, 0.74325557414624, 0.362645540999268,
                                                            3.80026640072534,
                                                            1.40930621014813)
                                        )
ggplot() + 
  geom_sf(data = world, show.legend = F) + 
  geom_point(data = centroids, aes(x = lat, y = long, size = cum_GDP), alpha = 0.5) + 
  scale_size_area(max_size = 25, breaks = c(0.1, 1, 50, floor(max(centroids$cum_GDP)))) +
  theme_bw() 

Error in geom_sf(data = world, show.legend = F) :
object 'world' not found

1 Like

You are not showing from where are you getting the world object, this should contain the "simple features" required to render the map.

Thanks @andresrcs. I had loaded sf - have now included that in my reprex. Same error, unfortunately.

library(datapasta)
library(ggplot2)
library(sf)
#datapasta:: df_paste(head(centroids, 5))

df <- data.frame(stringsAsFactors=FALSE,
                                                    lat = c(12396114.3738155, 1111619.42307639, 325672.675245503,
                                                            -7235015.91810587,
                                                            -6432333.51408773),
                                                   long = c(-2670804.78590195, 5043993.78855193, 5406895.92108048,
                                                            6328945.06430578,
                                                            -3204964.2454797),
                                           country.code = c("AUS", "AUT", "BEL", "CAN", "CHL"),
                                                country = c("Australia", "Austria", "Belgium", "Canada", "Chile"),
                                                cum_GDP = c(0.998576308865479, 0.74325557414624, 0.362645540999268,
                                                            3.80026640072534,
                                                            1.40930621014813)
                                        )
ggplot() + 
  geom_sf(data = world, show.legend = F) + 
  geom_point(data = centroids, aes(x = lat, y = long, size = cum_GDP), alpha = 0.5) + 
  scale_size_area(max_size = 25, breaks = c(0.1, 1, 50, floor(max(centroids$cum_GDP)))) +
  theme_bw() 

Error in geom_sf(data = world, show.legend = F) :
object 'world' not found

world is not part of sf that is why you are getting that message, check again the instructions you are following, they should be getting this object from another package like this for example

library("rnaturalearth")
library("rnaturalearthdata")

world <- ne_countries(scale = "medium", returnclass = "sf")
1 Like

Thanks for your help, @andresrcs. I'm not following instructions - I actually got all of this code to work before without these additional lines. I followed your suggestion and am no longer getting an error, but am getting a figure that doesn't have a map in the background (see image).

 library(datapasta)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
#datapasta:: df_paste(head(centroids, 5))

df <- data.frame(stringsAsFactors=FALSE,
                                                    lat = c(12396114.3738155, 1111619.42307639, 325672.675245503,
                                                            -7235015.91810587,
                                                            -6432333.51408773),
                                                   long = c(-2670804.78590195, 5043993.78855193, 5406895.92108048,
                                                            6328945.06430578,
                                                            -3204964.2454797),
                                           country.code = c("AUS", "AUT", "BEL", "CAN", "CHL"),
                                                country = c("Australia", "Austria", "Belgium", "Canada", "Chile"),
                                                cum_GDP = c(0.998576308865479, 0.74325557414624, 0.362645540999268,
                                                            3.80026640072534,
                                                            1.40930621014813)
                                        )

world <- ne_countries(returnclass = "sf")

ggplot() + 
  geom_sf(data = world, show.legend = F) + 
  geom_point(data = centroids, aes(x = lat, y = long, size = cum_GDP), alpha = 0.5) + 
  scale_size_area(max_size = 25, breaks = c(0.1, 1, 50, floor(max(centroids$cum_GDP)))) +
  theme_bw() 

This is do to the format of your coordinates the map uses longitude-latitude coordinates and I think your dots are UTM coordinates.

I don't know the right projection (this is why the dots are not in the right place) but it should be something similar to this

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgdal)

df <- data.frame(stringsAsFactors=FALSE,
                 lat = c(12396114.3738155, 1111619.42307639, 325672.675245503,
                         -7235015.91810587,
                         -6432333.51408773),
                 long = c(-2670804.78590195, 5043993.78855193, 5406895.92108048,
                          6328945.06430578,
                          -3204964.2454797),
                 country.code = c("AUS", "AUT", "BEL", "CAN", "CHL"),
                 country = c("Australia", "Austria", "Belgium", "Canada", "Chile"),
                 cum_GDP = c(0.998576308865479, 0.74325557414624, 0.362645540999268,
                             3.80026640072534,
                             1.40930621014813)
)

df <- spTransform(SpatialPoints(cbind(df$lat,df$long), proj4string=CRS("+proj=utm +zone=30")),CRS("+proj=longlat")) %>% 
    as.data.frame() %>%
    cbind(df)

world <- ne_countries(returnclass = "sf")

ggplot() + 
    geom_sf(data = world, show.legend = F) + 
    geom_point(data = df, aes(x = coords.x2, y = coords.x1, size = cum_GDP), alpha = 0.5) + 
    theme_bw()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.