2

I'm following a tutorial to create a cartogram and the line to do so creates an error, as does the solution to fix the error which is to transform the data using st_transform(). I've looked online for similar issues/fixes and can't seem to find much.

Initial Code:

library(cartogram)    
library(maptools)     # world boundaries coordinates

data(wrld_simpl)
afr=wrld_simpl[wrld_simpl$REGION==2,]

plot(afr)
afr@data

#Line causing problems
afr_cartogram <- cartogram_cont(afr, "POP2005", itermax=7)

Initial Error:

> afr_cartogram <- cartogram_cont(afr, "POP2005", itermax=7)
Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection.

Code trying to fix previous error by calling st_Transform():

afr_proj <- st_transform(afr, "+init=epsg:4326")
afr_cartogram <- cartogram_cont(afr_proj, "POP2005", itermax=7)

New Error:

> afr_proj <- st_transform(afr, "+init=epsg:4326")
Error in st_transform(afr, "+init=epsg:4326") : 
  could not find function "st_transform"
> afr_cartogram <- cartogram_cont(afr_proj, "POP2005", itermax=7)
Error in cartogram_cont(afr_proj, "POP2005", itermax = 7) : 
  object 'afr_proj' not found
2
  • 1
    You don't need most of those packages to show your problem. All you really need is maptools for the data and cartogram for the error. Editing your question to remove the unneeded packages will encourage people to help.
    – Spacedman
    Commented Dec 28, 2019 at 9:16
  • sorry new here (and to this in general), thanks for the tip!
    – rikki
    Commented Dec 29, 2019 at 7:42

1 Answer 1

5

There are two different and mostly incompatible ways of storing spatial data in R - the sp package which works with objects with names like SpatialPolygonsDataFrame and the sf package which creates objects which we tend to call "sf data frames" or "simple features data frames".

What's happened here is that afr is...

> class(afr)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

from the sp package, but the error message suggesting using st_transform is giving you a function that only works with the other type of data frame, from the sf package.

This is because the cartogram package can handle either type of spatial data as input, and does this by converting the sp object to an sf object and then passing that to its code that works with sf objects. When it then detects the problem with the projection, the error message suggests the sf way of transforming, since by then its forgotten the original object was an sp object. Oopsie.

Two ways round this..

Method 1: convert your data to an sf object and work with that. I'd recommend this since sf objects have several advantages over sp. Attach the sf package and convert:

> library(sf)
> afr_sf = st_as_sf(afr)

Now the cartogram function still fails with this new object:

> afr_cartogram <- cartogram_cont(afr_sf, "POP2005", itermax=7)
Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection.

But at least the error message suggests something that works:

> afr_sf_proj = st_transform(afr_sf,4326)
>

Not that 4326 is a good choice, since its the same lat-long coordinates that the first one was in.

Africa is so large its hard to find a good projection, so you may as well use Google Mercator:

> afr_sf_proj = st_transform(afr_sf,3857)

which at least gets you something that works:

> afr_cartogram <- cartogram_cont(afr_sf_proj, "POP2005", itermax=7)
Mean size error for iteration 1: 5.79263926219508
Mean size error for iteration 2: 4.94711300669561
Mean size error for iteration 3: 4.32566127362516
[etc]

Method 2 is keeping the sp object as an sp object, using spTransform:

> afr_sp_proj = spTransform(afr, "+init=epsg:3857")
> afr_cartogram <- cartogram_cont(afr_sp_proj, "POP2005", itermax=7)
Mean size error for iteration 1: 5.79263926219508
Mean size error for iteration 2: 4.94711300669563
[etc]
1
  • Thanks, this makes more sense now. What are the cartogram iterations for and is there any trick to figure out how many to specify?
    – rikki
    Commented Dec 29, 2019 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.