2

ggplot2冲积层

我使用 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% 得到一个奇怪的值。

4

3 回答 3

1

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

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 包(v0.3.0)于 2020-05-19 创建

于 2020-05-19T14:11:46.797 回答
0

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

于 2020-05-19T14:04:18.430 回答
0

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

library(ggplot2)

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

reprex 包(v0.3.0)于 2020-05-19 创建

于 2020-05-19T14:05:53.310 回答