2

I am new to Spatial data & cartogram lib and getting some issues while trying to recreate plot from: https://www.r-graph-gallery.com/a-smooth-transition-between-chloropleth-and-cartogram.html

enter image description here

Lib & Data

library(tidyverse)
library(maptools)
library(cartogram)
library(viridis)
library(sf)

data("wrld_simpl")

afr_cartogram = wrld_simpl[wrld_simpl$REGION==2,]

After this, I had some error: like st_transform ..... which I fixed it after some googling using sf lib.

afr_sf <- st_as_sf(afr_cartogram)

afr_sf_proj = st_transform(afr_sf,3857)

afr_plot <- cartogram::cartogram(afr_sf_proj, "POP2005", itermax =7)

ISSUE: Now after this step I am unable to recreate the code as it is in the demo website as I do not have column group in my data.

ggplot() +
  geom_polygon(data = afr_plot, aes(fill = POP2005/1000000, x = LON, y = LAT, group = group) , size=0, alpha=0.9) +
  theme_void()

From where can I get group column ???

Code used in website:

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

afr_cartogram <- cartogram(afr, "POP2005", itermax=7)

# Transform these 2 objects in dataframe, plotable with ggplot2
afr_cartogram_df <- tidy(afr_cartogram) %>% left_join(. , afr_cartogram@data, by=c("id"="ISO3")) 
afr_df <- tidy(afr) %>% left_join(. , afr@data, by=c("id"="ISO3")) 
 
# And using the advices of chart #331 we can custom it to get a better result:
ggplot() +
  geom_polygon(data = afr_df, aes(fill = POP2005/1000000, x = long, y = lat, group = group) , size=0, alpha=0.9) +
  theme_void() +
  scale_fill_viridis(name="Population (M)", breaks=c(1,50,100, 140), guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1)) +
  labs( title = "Africa", subtitle="Population per country in 2005" ) +
  ylim(-35,35) +
  theme(
    text = element_text(color = "#22211d"), 
    plot.background = element_rect(fill = "#f5f5f4", color = NA), 
    panel.background = element_rect(fill = "#f5f5f4", color = NA), 
    legend.background = element_rect(fill = "#f5f5f4", color = NA),
    plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 13, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    legend.position = c(0.2, 0.26)
  ) +
  coord_map()
Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
ViSa
  • 1,563
  • 8
  • 30

1 Answers1

1

The group columns are produced in these lines

afr_cartogram_df <- tidy(afr_cartogram) %>% 
   left_join(afr_cartogram@data, by = ("id" = "ISO3")) 
afr_df <- tidy(afr) %>% 
   left_join(afr@data, by = c("id" = "ISO3"))

by the tidy function from package broom which is not attached in your code!

Attach broom using library(broom) or call tidy() from its namespace like this: broom::tidy(...).

The 'data section' in your code should look like this:

data(wrld_simpl)
afr <- wrld_simpl[wrld_simpl$REGION==2, ]
afr_cartogram <- wrld_simpl[wrld_simpl$REGION == 2,]
afr_sf <- st_as_sf(afr_cartogram)
afr_sf_proj <- st_transform(afr_sf,  3857)
afr_plot <- cartogram_cont(afr_sf_proj, "POP2005", itermax =7)

afr_cartogram_df <- broom::tidy(afr_cartogram) %>% 
  left_join(afr_cartogram@data, by=c("id" = "ISO3")) 
afr_df <- broom::tidy(afr) %>% 
  left_join(afr@data, by=c("id" = "ISO3")) 

The subsequent ggplot code works fine then:

enter image description here

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • Hi, since your answer gives a much better explanation, I deleted mine and added the code here. If you don't want it in, feel free to take it out. – slamballais May 17 '21 at 13:58
  • Thanks @slamballais for helping me out on this. I Appreciate your help :) – ViSa May 17 '21 at 14:03
  • Thanks @Martin C. Arnold for helping me out on this. I Appreciate your help :) – ViSa May 17 '21 at 14:03
  • @slamballais thanks for the suggestion, I just added a few lines and the ggplot output (: – Martin C. Arnold May 17 '21 at 14:07
  • Hi @MartinC.Arnold i am little confused here as what are you referring `afr` as in **your code** which is used in line `afr_df <- tidy(afr) %>% `. As you have used `afr_cartogram <- wrld_simpl[wrld_simpl$REGION == 2,]` but I don't see the object `afr` creation anywhere in those lines but they are called in the code later. – ViSa May 17 '21 at 14:30
  • You're right. I've added those lines to my answer. – Martin C. Arnold May 17 '21 at 14:36
  • Oh they both are same thing. I didn't know. Thanks for adding :) – ViSa May 17 '21 at 14:39