2

I'm new to using tmap. I'm using the default "World" map and I'm able to create a world map and color all the countries according to life expectancy simply using:

library(tmap)
library(sf)
tm_shape(World) +
  tm_polygons("life_exp") 

But now, I've created my own dataframe to merge with the World dataframe.

World2 <- merge(World, df, by="iso_a3")

This works and I now have a nice merged dataframe that includes my variables. But when I got to run it, I get this:

Error: Object World2 is neither from class sf, stars, Spatial, Raster, nor SpatRaster.

Is there something special I need to do to the merged dataframe to make it work here?

lovalery
  • 4,524
  • 3
  • 14
  • 28
Jarvis
  • 31
  • 5
  • Try step 8.1 here: https://rpubs.com/quarcs-lab/tutorial-maps-in-r (but also look at merge in step 8). – joncgoodwin Oct 14 '21 at 21:13
  • @user2619203, I see in step 8 they mention converting it to sf using st_as_sf() as opposed to st_sf() mentioned by lovalery below. I just tried and both work--thanks! – Jarvis Oct 14 '21 at 23:22

1 Answers1

0

You need to convert your dataframe into a sf type object. I guess the following line should solve your problem :

World2 <- st_sf(World2)

lovalery
  • 4,524
  • 3
  • 14
  • 28
  • that worked like a charm-thanks! I used World2 <- st_sf(merge(df, World, by="iso_a3")) – Jarvis Oct 14 '21 at 23:06
  • Glad that I could help you. Please mark the answer as accepted to make it easier for other users to find the right answers. Cheers – lovalery Oct 14 '21 at 23:07