最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ggplot2 - Dorling cartograms in R: suppressing normalization? - Stack Overflow

programmeradmin1浏览0评论

In R I am creating different versions of a cartogram, using the same countries but different values of the weight factors. In my example I create two cartograms, where the second cartogram has double the weights compared to the first one. Surprisingly, in the two resulting cartograms, the bubbles of each country are exactly the same size. That is, there seems to be some normalization of bubble sizes going on in cartogram_dorling. Of course I can indicate the different maximum sizes in the legend but I am expecting two cartograms where the bubbles are visually half the size of each other.

Here is a rather minimal code example:

library(sf)
library(cartogram)
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
library(dplyr)  

world <- ne_countries(scale = "medium", returnclass = "sf")
world <- st_transform(world, crs = 3395)

# First cartogram: (100, 20) scenario
weights_1 <- data.frame(
  country = c("Germany", "France"),
  weight = c(100, 20)
)
world_weights_1 <- world %>%
  filter(admin %in% c("Germany", "France")) %>%
  left_join(weights_1, by = c("admin" = "country"))
cartogram_1 <- cartogram_dorling(world_weights_1, weight = "weight")

p1 <- ggplot(cartogram_1) +
  geom_sf(aes(size = weight), color = "black") +
  theme_minimal() 
ggsave("cartogram_1.png", plot = p1, width = 8, height = 6, dpi = 300)

# Second cartogram: (200, 40) scenario
weights_2 <- data.frame(
  country = c("Germany", "France"),
  weight = c(200, 40)
)
world_weights_2 <- world %>%
  filter(admin %in% c("Germany", "France")) %>%
  left_join(weights_2, by = c("admin" = "country"))
cartogram_2 <- cartogram_dorling(world_weights_2, weight = "weight")

p2 <- ggplot(cartogram_2) +
  geom_sf(aes(size = weight), color = "black") +
  theme_minimal() 
ggsave("cartogram_2.png", plot = p2, width = 8, height = 6, dpi = 300)

I have not found a way to properly suppress the normalization and make the country bubbles in the first cartogram appear half as big as in the second cartogram. I only found the dirty workaround of adding an artificial transparent extra country bubble of size 200 in the first cartogram.

Is there a more elegant solution to fix this?

In R I am creating different versions of a cartogram, using the same countries but different values of the weight factors. In my example I create two cartograms, where the second cartogram has double the weights compared to the first one. Surprisingly, in the two resulting cartograms, the bubbles of each country are exactly the same size. That is, there seems to be some normalization of bubble sizes going on in cartogram_dorling. Of course I can indicate the different maximum sizes in the legend but I am expecting two cartograms where the bubbles are visually half the size of each other.

Here is a rather minimal code example:

library(sf)
library(cartogram)
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
library(dplyr)  

world <- ne_countries(scale = "medium", returnclass = "sf")
world <- st_transform(world, crs = 3395)

# First cartogram: (100, 20) scenario
weights_1 <- data.frame(
  country = c("Germany", "France"),
  weight = c(100, 20)
)
world_weights_1 <- world %>%
  filter(admin %in% c("Germany", "France")) %>%
  left_join(weights_1, by = c("admin" = "country"))
cartogram_1 <- cartogram_dorling(world_weights_1, weight = "weight")

p1 <- ggplot(cartogram_1) +
  geom_sf(aes(size = weight), color = "black") +
  theme_minimal() 
ggsave("cartogram_1.png", plot = p1, width = 8, height = 6, dpi = 300)

# Second cartogram: (200, 40) scenario
weights_2 <- data.frame(
  country = c("Germany", "France"),
  weight = c(200, 40)
)
world_weights_2 <- world %>%
  filter(admin %in% c("Germany", "France")) %>%
  left_join(weights_2, by = c("admin" = "country"))
cartogram_2 <- cartogram_dorling(world_weights_2, weight = "weight")

p2 <- ggplot(cartogram_2) +
  geom_sf(aes(size = weight), color = "black") +
  theme_minimal() 
ggsave("cartogram_2.png", plot = p2, width = 8, height = 6, dpi = 300)

I have not found a way to properly suppress the normalization and make the country bubbles in the first cartogram appear half as big as in the second cartogram. I only found the dirty workaround of adding an artificial transparent extra country bubble of size 200 in the first cartogram.

Is there a more elegant solution to fix this?

Share Improve this question edited Nov 18, 2024 at 18:31 SamR 20.9k4 gold badges19 silver badges48 bronze badges asked Nov 18, 2024 at 17:46 CampanulaCampanula 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

Dorling cartograms are designed to represent proportional relationships, not absolute differences in size. The size of the circles are relative to one another. So if you have weights of c(100, 20) in one data frame and c(200, 40) for the same countries in another data frame, the relative proportions between the bubbles remain the same, and the maps will look identical. This is expected behaviour.

You can adjust the overall scale of the bubbles using the k parameter in cartogram::cartogram_dorling():

k: Share of the bounding box of x filled by the larger circle

The default is 5. You can adjust this to make the bubbles larger or smaller across the entire map, until you achieve your desired circle size:

k_values <- c(1, 2.5, 5, 10)
lapply(
    k_values,
    \(k) cartogram_dorling(world_weights_1, weight = "weight", k = k) |>
        ggplot() +
        geom_sf(aes(size = weight, fill = admin)) +
        theme_minimal() +
        guides(size = "none") +
        theme(legend.position = "bottom", legend.title = element_blank()) +
        labs(title = sprintf("K = %s", k))
) |>
    patchwork::wrap_plots()

While this works, I don't think this type of comparison is clear. In particular, the k=10 circles do not look twice as large to me as k=5. If you want to compare absolute differences between countries, I would recommend using a different type of visualisation altogether, like a bar chart. Comparing the size of circles between Dorling cartograms faces the same problem as pie charts, i.e. it is very difficult for humans to accurately gauge the size of circles.

发布评论

评论列表(0)

  1. 暂无评论