如何调整饼图上的 ggrepel 标签?

How to adjust ggrepel label on pie chart?

我正在尝试创建一个饼图来可视化 9 个属的丰度百分比。然而,标签都聚集在一起。我该如何补救?代码如下:

generaabundance2020 <- c(883, 464, 1948, 1177, 2607, 962, 2073, 620, 2670)

genera2020 <-  c("Andrena", "Ceratina", "Halictus", 
                 "Hesperapis", "Lasioglossum", "Melissodes", 
                 "Osmia", "Panurginus", "Other")

generabreakdown2020 <- data.frame(group = genera2020, value = generaabundance2020)

gb2020label <- generabreakdown2020 %>% 
  group_by(value) %>% # Variable to be transformed
  count() %>% 
  ungroup() %>% 
  mutate(perc = `value` / sum(`value`)) %>% 
  arrange(perc) %>%
  mutate(labels = scales::percent(perc))

generabreakdown2020 %>%
  ggplot(aes(x = "", y = value, fill = group)) +
  geom_col() + 
  coord_polar("y", start = 0) +
  theme_void() +
  geom_label_repel(aes(label = gb2020label$labels), position = position_fill(vjust = 0.5), 
                   size = 5, show.legend = F, max.overlaps = 50) +
  guides(fill = guide_legend(title = "Genera")) +
  scale_fill_manual(values = c("brown1", "chocolate1",
                               "darkgoldenrod1", "darkgreen",
                               "deepskyblue", "darkslateblue",
                               "darkorchid4", "hotpink1",
                               "lightpink"))

产生以下内容:

您没有向我们提供您的工作数据,所以我在这里使用 ggplot2::mpg

library(tidyverse)
library(ggrepel)

mpg_2 <- 
  mpg %>% 
  slice_sample(n = 20) %>% 
  count(manufacturer) %>% 
  mutate(perc = n / sum(n)) %>% 
  mutate(labels = scales::percent(perc)) %>% 
  arrange(desc(manufacturer)) %>% 
  mutate(text_y = cumsum(n) - n/2)

没有极坐标的图表

mpg_2 %>%    
  ggplot(aes(x = "", y = n, fill = manufacturer)) + 
  geom_col() +
  geom_label(aes(label = labels, y = text_y))

带有极坐标和 geom_label_repel

的图表
mpg_2 %>%    
  ggplot(aes(x = "", y = n, fill = manufacturer)) + 
  geom_col() +
  geom_label_repel(aes(label = labels, y = text_y), 
                   force = 0.5,nudge_x = 0.6, nudge_y = 0.6) +
  coord_polar(theta = "y")

但也许您的数据不够密集,需要排斥?

mpg_2 %>%    
  ggplot(aes(x = "", y = n, fill = manufacturer)) + 
  geom_col() +
  geom_label(aes(label = labels, y = text_y), nudge_x = 0.6) +
  coord_polar(theta = "y")

reprex package (v2.0.1)

于 2021-10-26 创建

感谢您添加数据。

您的代码中存在一些错误。最主要的是您没有预先计算放置标签的位置(在 text_y 变量中完成)。该变量需要作为 geom_label_repel.

的 y 美学传递

第二个是你不再需要 group_by(value) %>% count() %>% ungroup()因为您提供的数据已经汇总。

library(tidyverse)
library(ggrepel)

generaabundance2020 <- c(883, 464, 1948, 1177, 2607, 962, 2073, 620, 2670)
genera2020 <-  c("Andrena", "Ceratina", "Halictus", "Hesperapis", "Lasioglossum", "Melissodes", "Osmia", "Panurginus", "Other")
generabreakdown2020 <- data.frame(group = genera2020, value = generaabundance2020)

gb2020label <- 
  generabreakdown2020 %>% 
  mutate(perc = value/ sum(value)) %>% 
  mutate(labels = scales::percent(perc)) %>% 
  arrange(desc(group)) %>% ## arrange in the order of the legend
  mutate(text_y = cumsum(value) - value/2) ### calculate where to place the text labels

gb2020label %>%
  ggplot(aes(x = "", y = value, fill = group)) + 
  geom_col() +
  coord_polar(theta = "y") +
  geom_label_repel(aes(label = labels, y = text_y), 
                   nudge_x = 0.6, nudge_y = 0.6,
                   size = 5, show.legend = F) +
  guides(fill = guide_legend(title = "Genera")) +
  scale_fill_manual(values = c("brown1", "chocolate1",
                               "darkgoldenrod1", "darkgreen",
                               "deepskyblue", "darkslateblue",
                               "darkorchid4", "hotpink1",
                               "lightpink"))

如果要按频率降序排列,请记住将组变量的因子水平也设置为相同的顺序。

gb2020label <- 
  generabreakdown2020 %>% 
  mutate(perc = value/ sum(value)) %>% 
  mutate(labels = scales::percent(perc)) %>% 
  arrange(desc(perc)) %>% ## arrange in descending order of frequency
  mutate(group = fct_rev(fct_inorder(group))) %>% ## also arrange the groups in descending order of freq
  mutate(text_y = cumsum(value) - value/2) ### calculate where to place the text labels

gb2020label %>%
  ggplot(aes(x = "", y = value, fill = group)) + 
  geom_col() +
  coord_polar(theta = "y") +
  geom_label_repel(aes(label = labels, y = text_y), 
                   nudge_x = 0.6, nudge_y = 0.6,
                   size = 5, show.legend = F) +
  guides(fill = guide_legend(title = "Genera")) +
  scale_fill_manual(values = c("brown1", "chocolate1",
                               "darkgoldenrod1", "darkgreen",
                               "deepskyblue", "darkslateblue",
                               "darkorchid4", "hotpink1",
                               "lightpink"))

reprex package (v2.0.1)

于 2021-10-27 创建