I am trying to create a cartogram using the R cartogram package. My code looks as follows:

#Create the dataframe
data(wrld_simpl)
world_map4 <- wrld_simpl

news2 <- news %>% 
  group_by(iso_a3) %>% 
  select(iso_a3, Mentions_sum, Goldstein_avg) %>% 
  slice(1) %>% 
  filter(!iso_a3=="NA")

world_map5 <- merge(world_map4, news2, by.x = "ISO3", by.y = "iso_a3", all.x = TRUE, duplicateGeoms=TRUE)
world_map6 <- st_as_sf(world_map5)
world_map7 <- st_transform(world_map6, crs = 23038)

#Draw the map
world_map8 <- world_map7[!is.na(world_map7$Mentions_sum), ]
world_cartogram <- cartogram_cont(world_map8, "Mentions_sum", itermax=7)

Executing the last line gives me the following error: Error in Fij[distance <= radius[j]] <- Fbij[distance <= radius[j]] : NAs are not allowed in subscripted assignments

I checked:

> any(is.na(world_map8$Mentions_sum))
[1] FALSE

and I checked:

> nrow(world_map8)
[1] 155

and I checked:

> summary(world_map8$Mentions_sum)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    6.00   18.00   76.83   59.00 1060.00 

Don't know what to do further. Does anyone has an idea?

1

There are 1 best solutions below

0
On

Found the issue. 23038 is not projecting all countries, leaving empty polygons e.g. for Brazil.

Projection 3857 works.