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

我正在尝试创建一个饼图来可视化 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"))

产生以下内容:

最佳答案

感谢您添加数据。

您的代码中存在一些错误。最主要的是您没有预先计算放置标签的位置(在此处的 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 创建于 2021-10-27 (v2.0.1)

https://stackoverflow.com/questions/69715282/

相关文章:

julia - 如何在 Julia 中生成随机日期?

r - 如何在 ifelse 中设置 FALSE 条件以使其保持原始值

html - 是否可以使用纯 CSS 为仅包含一个元素的列表设置样式,而不是为包含多个元素的列表设置

java - 将 Enum 类名本身作为 String 获取的最佳方法

prototyp">javascript - 在 javascript "contructor --> prototyp

c++ - std::vector 和 move 语义

spring-boot - 应用程序通过 Spring Boot Run 而不是通过 Intelli

typescript - 如何从Nuxt 3服务器获取路由参数

typescript - 类型错误 : EventEmitter is not a construc

r - 在 download.file 中关闭 SSL 验证