0

我想在我正在创建的 ggalluvial 图中使用 geom_text。下面提供的代码按预期工作,除非性能级别之间存在 50/50 的平均分配。geom_text() 中是否有一个参数来防止文本在 geom_stratum(aes(fill = profEOY)) 上重复?简而言之,2018 年的结果看起来如预期,2019 年的结果与 50/50 的比例是有问题的。

我在 Shiny 中为 160 多个报告网站展示了这个图,所以我认为 annotate() 不会起作用。

示例图

library(tidyverse)
library(ggalluvial)
df <-
  structure(
    list(
      profBOY = c(
        "At or Above",
        "At or Above",
        "Below or Well Below",
        "Below or Well Below",
        "At or Above",
        "At or Above",
        "Below or Well Below",
        "Below or Well Below"
      ),
      profEOY = c(
        "At or Above",
        "Below or Well Below",
        "At or Above",
        "Below or Well Below",
        "At or Above",
        "Below or Well Below",
        "At or Above",
        "Below or Well Below"
      ),
      EndYear = c(
        2018L,
        2018L,
        2018L,
        2018L,
        2019L,
        2019L,
        2019L,
        2019L
      ),
      sum = c(37, 0, 6, 21, 27, 3, 5, 22),
      totaln = c(74L,
                 74L, 74L, 74L, 80L, 80L, 80L, 80L),
      totalNBoy = c(68, 68, 68, 68, 64, 64, 64, 64),
      totalInBoyPB = c(
        "38",
        "38",
        "30",
        "30",
        "32",
        "32",
        "32",
        "32"
      ),
      percentInBoyPB = c(55.9, 55.9, 44.1, 44.1,
                         50, 50, 50, 50),
      totalNEoy = c(69, 69, 69,
                    69, 73, 73, 73, 73),
      totalInEoyPB = c(
        "44",
        "25",
        "44",
        "25",
        "34",
        "39",
        "34",
        "39"
      ),
      percentInEoyPB = c(63.8, 36.2, 63.8, 36.2, 46.6, 53.4, 46.6, 53.4),
      percent = c(
        "50%",
        "0%",
        "8%",
        "28%",
        "34%",
        "4%",
        "6%",
        "28%"
      )
    ),
    row.names = c(NA,-8L),
    class = c("tbl_df", "tbl", "data.frame")
  )

ggplot(df,
       aes(y = sum,
           axis1 = str_wrap(profBOY, 10),
           axis2 = str_wrap(profEOY, 10),
           fill = profBOY
       )) +
  geom_flow(color = '#e57a3c', curve_type = 'quintic') +
  scale_x_discrete(limits = c("Beginning \nof Year", "End \nof Year")) +
  scale_fill_manual(values = c("#315683","#6c7070")) +
  geom_stratum(aes(fill = profEOY), color = 'grey', width = 1/2) +
  geom_stratum(aes(fill = profBOY), color = 'grey', width = 1/2) +
  geom_text(stat = 'stratum', 
            aes(label = paste0(percentInBoyPB, '%')), 
            vjust = 1, size = 4, color = 'white')+
  labs(fill = 'Performance Level')+
  facet_wrap(vars(factor(EndYear)), 
             nrow = 1, 
             scales = 'free_y')+
  theme_minimal()+
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks = element_blank(),
        panel.grid = element_blank(), 
        legend.position = 'top', 
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        axis.text.x = element_text(size = 16), 
        strip.text = element_text(size = 18), 
        strip.background = element_rect(fill = 'lightgrey', color = 'lightgrey')
  )

感谢您的任何建议!

4

1 回答 1

0

为了解决这个问题,我认为“最干净”的想法是将数据转换为“长”(或 Lodes)格式。这使得层标签更容易控制。

还有另一种方法来控制这一点,使用after_stat()函数恢复通过统计转换获得的变量以构建冲积层。

geom_text()if you writeaes(label = after_stat(x))中,层将具有以下标签: Alluvial_plot_example_1

您现在可以看到x写在各自层上的变量。我们还可以看到x对应于冲积轴:第一个轴(年初)为“1”,第二个轴(年末)为“2”。

ifelse()知道了这一点,你现在可以用一个简单的语句来控制你想写的标签。

总之,我的解决方案包括geom_text()通过写作aes(label = ifelse(test = after_stat(x) == "1", paste0(df$percentInBoyPB, '%'), ""))来修改美学以获得:

Alluvial_plot_example_2

整个ggplot是:

ggplot(df,
       aes(y = sum,
           axis1 = str_wrap(profBOY, 10),
           axis2 = str_wrap(profEOY, 10),
           fill = profBOY
       )) +
  geom_flow(color = '#e57a3c', curve_type = 'quintic') +
  scale_x_discrete(limits = c("Beginning \nof Year", "End \nof Year")) +
  scale_fill_manual(values = c("#315683","#6c7070")) +
  geom_stratum(aes(fill = profEOY), color = 'grey', width = 1/2) +
  geom_text(stat = 'stratum', 
            aes(label = ifelse(test = after_stat(x) == "1", paste0(df$percentInBoyPB, '%'), "")), 
            vjust = 1, size = 4, color = 'white')+
  labs(fill = 'Performance Level')+
  facet_wrap(vars(factor(EndYear)), 
             nrow = 1, 
             scales = 'free_y')+
  theme_minimal()+
  theme(axis.text.y = element_blank(), 
        axis.title.y = element_blank(), 
        axis.ticks = element_blank(),
        panel.grid = element_blank(), 
        legend.position = 'top', 
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 14),
        axis.text.x = element_text(size = 16), 
        strip.text = element_text(size = 18), 
        strip.background = element_rect(fill = 'lightgrey', color = 'lightgrey')
  )
于 2021-11-17T15:55:53.017 回答