ggplot2地理分布叠加饼图

Tidytuesday 2024 Week 22

ggplot2
Tidytuesday
Author

Lee

Published

July 16, 2024

1 准备数据

library(rnaturalearthdata)
library(rnaturalearth)
library(ggplot2)
library(tidyverse)
library(rgeos)
library(ggstar)
library(scatterpie)
library(ggnewscale)
library(readxl)

world_map <- map_data("world") 
world_map_test <- world_map %>% 
  group_by(region) %>% 
  slice_head(n = 1) %>% 
  select(long, lat)
file_1 <- "D:/Myblog/posts/ggplot2-tidytuesday-2024week22/tech/41588_2021_831_MOESM4_ESM.xlsx"
fig1a_dat <- read_excel(file_1, sheet = "Fig1a")

test <- fig1a_dat %>%
  rename(region = "Country", radius = "Total number") %>%
  mutate(radius_1 = radius / 10) %>%
  left_join(world_map_test, by = "region") %>%
  select(long, lat, everything()) %>%
  janitor::clean_names()

ggplot() +
  geom_scatterpie(aes(x = long, y = lat, group = region, r = radius_1),
    data = test,
    cols = c("l_sativa", "l_serriola", "l_saligna", "l_virosa")
  ) +
  coord_equal() +
  theme_bw()

p <- ggplot() +
  geom_polygon(
    data = world_map, aes(x = long, y = lat, group = group),
    fill = "#dedede"
  ) +
  theme_bw() +
  scale_y_continuous(expand = expansion(mult = c(0, 0))) +
  scale_x_continuous(expand = expansion(add = c(0, 0))) +
  theme(
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  ) +
  labs(x = NULL, y = NULL)

p1 <- p + geom_scatterpie(
  aes(x = long, y = lat, group = region, r = radius_1),
  data = test, cols = c("l_sativa", "l_serriola", "l_saligna", "l_virosa"),
  color = NA, alpha = .8
) +
  coord_sf(
    xlim = c(-10, 90.00),
    ylim = c(20.00, 90.00),
    expand = FALSE
  ) +
  geom_scatterpie_legend(test$radius_1,
    x = 50, y = 30, n = 3
  )

p1