I'm trying to use the wrld_simple data from the maptools package to plot a Cartogram that involves some European countries based on their Population in 2005:

countries = c('Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom')

Using the maptools package, I got the wrld_simpl data, and retrieved the data for those particular countries:

data(wrld_simpl)

mapsimple = wrld_simpl[wrld_simpl$NAME %in% countries]

plot(mapsimple)

So far so good, but when I plot it out:

cartogram= cartogram(mapping1, "POP2005", itermax = 7)

Please use cartogram_cont() instead of cartogram().

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.

I try using cartogram_cont:

> cartogram= cartogram_cont(mapping1, "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.

I tried transforming it to sf and then doing the st_transform(), but to no avail:

sfno = st_as_sf(mapping1)

sfnoproj = st_transform(sfno, coords = c("lon", "lat"), crs = "+proj=longlat +datum=WGS84", agr = "constant")

cartogram= cartogram_cont(sfnoproj, "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.

I'm assuming I don't have the correct values for the projection right. How would I know to set it better? If I were to use other values besides 'POP2005', how do I go about it? What does the 'itermax' in cartogram mean (I don't quite understand the given definition)

Any help would be greatly appreciated as I'm quite new to this. Thank you!!!!

1

There are 1 best solutions below

1
Eugene Chong On

Some inconsistencies in your sample data (mapsimple v. mapping1 and a missing comma), so updated below:

library(maptools)
library(sf)
library(ggplot2)

countries = c('Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom')

data(wrld_simpl)

mapsimple = wrld_simpl[wrld_simpl$NAME %in% countries,]

As the error indicated, your data was unprojected. It uses latitude/longitude, which doesn't work for cartogram_cont().

sfno <- st_as_sf(mapsimple)
st_crs(sfno)

Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Your st_transform() was reprojecting sfno as the same thing: EPSG 4326:

sfnoproj <- st_transform(sfno, coords = c("lon", "lat"), 
                        crs = "+proj=longlat +datum=WGS84", 
                        agr = "constant")
st_crs(sfnoproj)

Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Instead, choose a projected coordinate system for Europe, like 23038.

sfproj <- st_transform(sfno, crs = 23038)
st_crs(sfproj)

Coordinate Reference System:
  EPSG: 23038 
  proj4string: "+proj=utm +zone=38 +ellps=intl +towgs84=-87,-98,-121,0,0,0,0 +units=m +no_defs"

Then, we can make and plot the cartogram:

cartogram <- cartogram_cont(sfproj, "POP2005", itermax = 7)

ggplot() +
  geom_sf(data = cartogram, aes(fill = POP2005))

enter image description here