【问题标题】:How to adjust ggrepel label on pie chart?如何调整饼图上的ggrepel标签?
【发布时间】:2021-10-25 22:05:26
【问题描述】:

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

这会产生以下内容:

【问题讨论】:

    标签: r ggplot2 ggrepel


    【解决方案1】:

    您没有向我们提供要使用的数据,所以我在这里使用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 日创建

    【讨论】:

    • 我进行了编辑以包含我的数据。我尝试了您建议的两种标签方法,并重新格式化了我的 gb2020labels 以匹配您的 mpg_2,但我仍然遇到相同的问题,即我的所有标签都聚集在一个位置。还有其他故障排除技巧吗?
    【解决方案2】:

    感谢您添加数据。

    您的代码中有一些错误。主要问题是您没有预先计算标签的放置位置(在text_y 变量中完成)。该变量需要作为 geom_label_repel 的 y 美学传递。

    第二个是你不再需要 group_by(value) %&gt;% count() %&gt;% 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 日创建

    【讨论】:

    • 不确定这是否值得单独回答... +1 表示共同努力
    • 非常感谢!现在都修好了。
    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多