You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中Dorling cartogram如何取消大小归一化?

问题:Dorling Cartogram气泡大小的跨图比例匹配

我使用R语言的cartogram包生成Dorling cartogram时,遇到一个问题:相同国家、权重为两倍关系的两幅图,气泡的视觉大小完全一致。推测cartogram_dorling()函数内部对气泡大小做了归一化处理,但我需要第一版图的气泡视觉大小为第二版的一半,而非仅在图例中标注数值差异。

以下是极简复现代码:

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)

# 第一版:权重(100, 20)
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)

# 第二版:权重(200, 40)
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)

优雅解决方案

方案1:绘图阶段手动控制缩放比例

利用ggplot2的size映射,手动调整权重的缩放系数,让两幅图的气泡绝对大小对应权重的倍数关系:

# 第一版图:将权重乘以0.5,使最大气泡视觉大小为第二版的一半
p1 <- ggplot(cartogram_1) +
  geom_sf(aes(size = weight * 0.5), color = "black") +
  # 保持图例显示原始权重数值
  scale_size_continuous(name = "Weight", breaks = c(20, 100), labels = c(20, 100)) +
  theme_minimal() 

# 第二版图:使用原始权重绘图
p2 <- ggplot(cartogram_2) +
  geom_sf(aes(size = weight), color = "black") +
  scale_size_continuous(name = "Weight", breaks = c(40, 200), labels = c(40, 200)) +
  theme_minimal() 

ggsave("cartogram_1_scaled.png", plot = p1, width = 8, height = 6, dpi = 300)
ggsave("cartogram_2_scaled.png", plot = p2, width = 8, height = 6, dpi = 300)

方案2:统一基准生成Cartogram

在生成cartogram前,将两组权重统一到同一基准下(比如把第一版权重乘以2),让cartogram_dorling()基于相同的尺寸规则生成气泡,再拆分绘图:

# 合并权重数据,第一版权重放大2倍,与第二版保持同基准
combined_weights <- bind_rows(
  weights_1 %>% mutate(weight_scaled = weight * 2, group = "version1"),
  weights_2 %>% mutate(weight_scaled = weight, group = "version2")
)

world_combined <- world %>%
  filter(admin %in% c("Germany", "France")) %>%
  left_join(combined_weights, by = c("admin" = "country"))

# 基于统一缩放后的权重生成cartogram
cartogram_combined <- cartogram_dorling(world_combined, weight = "weight_scaled")

# 绘制第一版,图例显示原始权重
p1 <- ggplot(cartogram_combined %>% filter(group == "version1")) +
  geom_sf(aes(size = weight), color = "black") +
  scale_size_continuous(name = "Weight", breaks = c(20, 100)) +
  theme_minimal()

# 绘制第二版,图例显示原始权重
p2 <- ggplot(cartogram_combined %>% filter(group == "version2")) +
  geom_sf(aes(size = weight), color = "black") +
  scale_size_continuous(name = "Weight", breaks = c(40, 200)) +
  theme_minimal()

ggsave("cartogram_1_unified.png", plot = p1, width = 8, height = 6, dpi = 300)
ggsave("cartogram_2_unified.png", plot = p2, width = 8, height = 6, dpi = 300)

内容的提问来源于stack exchange,提问作者Campanula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 03:50:14