如何在冲积或桑基图中获得 y 轴上的百分比?

How to get percentages on the y axes in an alluvial or sankey plot?

我使用 ggplot2 实现了这张图,我想将 y 轴更改为百分比,从 0% 到 100%,每 10 个中断一次。 我知道我可以使用:

+ scale_y_continuous(label=percent, breaks = seq(0,1,.1))

但我仍然遇到问题,因为转换为百分比时,R 将 30000 解释为 30000%,因此如果限制为 100%,我的图表中什么也看不到。 我该如何管理它?

我有这样一个数据集:

ID time value
1   1   B with G available
2   1   Generic
3   1   B with G available
4   1   Generic
5   1   B with G available
6   1   Generic
7   1   Generic
8   1   Generic
9   1   B with G available
10  1   B with G available
11  1   Generic
12  1   B with G available
13  1   B with G available
14  1   B with G available
15  1   Generic
16  1   B with G available
17  1   B with G available
18  1   B with G available
19  1   B with G available
20  1   B with G available
1   2   B with G available
2   2   Generic
3   2   B with G available
4   2   Generic
5   2   B with G available
6   2   Generic
7   2   Generic
8   2   Generic
9   2   B with G available
10  2   B with G available
11  2   Generic
12  2   B with G available
13  2   B with G available
14  2   B with G available
15  2   Generic
16  2   B with G available
17  2   switch
18  2   B with G available
19  2   B with G available
20  2   switch

可使用此代码重现:

PIPPO <- data.frame("ID"=rep(c(1:20),2), "time"=c(rep(1,20),rep(2,20)), "value"=c("B","G","B","G","B",rep("G",3),rep("B",2),"G",rep("B",3),"G",rep("B",6),"G","B","G","B",rep("G",3),rep("B",2),"G",rep("B",3),"G","B","switch",rep("B",2),"switch"))

所以我没有可以管理的 y 轴变量。

这是我的代码和我得到的情节

ggplot(PIPPO, 
       aes(x = time, stratum = value, alluvium = ID,
           fill = value, label = value)) +
  scale_fill_brewer(type = "qual" , palette = "Set3") +
  geom_flow(stat = "flow", knot.pos = 1/4, aes.flow = "forward",
            color = "gray") + 
  geom_stratum() +
  theme(legend.position = "bottom") 

有人能帮帮我吗?

我使用

获得的真实数据
scale_y_continuous(label = scales::percent_format(scale = 100 / n_id))

是这样的:

最大值为 84%(而非 100%)。我怎样才能使 y 轴达到 100% 并每 10% 断开一次?

这是我得到的

scale_y_continuous(breaks = scales::pretty_breaks(10), label = scales::percent_format(scale = 100 / n_id))

我每 14% 得到一个奇怪的值。

我假设您需要创建一个新的百分比列,方法是获取总行数,然后将列中的每个 "value" 除以总数,得到它代表的百分比。

简单地标准化您的 y 值似乎可以解决问题:

library(ggplot2)

ggplot(mtcars, aes(x = cyl, y = mpg/max(mpg))) +
  geom_point() +
  scale_y_continuous(label = scales::label_percent())

reprex package (v0.3.0)

于 2020-05-19 创建

使用 percent_format 中的 scale 参数可以这样实现:

PIPPO <- data.frame("ID"=rep(c(1:20),2), "time"=c(rep(1,20),rep(2,20)), "value"=c("B","G","B","G","B",rep("G",3),rep("B",2),"G",rep("B",3),"G",rep("B",6),"G","B","G","B",rep("G",3),rep("B",2),"G",rep("B",3),"G","B","switch",rep("B",2),"switch"))

library(ggplot2)
library(ggalluvial)

n_id <- length(unique(PIPPO$ID))

ggplot(PIPPO, 
       aes(x = time, stratum = value, alluvium = ID,
           fill = value, label = value)) +
  scale_fill_brewer(type = "qual" , palette = "Set3") +
  scale_y_continuous(label = scales::percent_format(scale = 100 / n_id)) +
  geom_flow(stat = "flow", knot.pos = 1/4, aes.flow = "forward", color = "gray",) + 
  geom_stratum() +
  theme(legend.position = "bottom") 

reprex package (v0.3.0)

于 2020-05-19 创建