如何获得冲积或sankey图中y轴上的百分比? [英] How to get percentages on the y axes in an alluvial or sankey plot?
问题描述
我意识到使用ggplot2绘制了这张图,我想将y轴的百分比从0%更改为100%,每10个中断一次. 我知道我可以使用:
I realized this graph using ggplot2 and I'd like to change y axes to percentages, from 0% to 100% with breaks every 10. I know I can use:
+ scale_y_continuous(label=percent, breaks = seq(0,1,.1))
但我仍然遇到问题,因为将百分比转换为R会将30000解释为30000%,因此,如果限制为100%,我的图形中什么也不会得到. 我该如何管理?
but I still get a problem because, turning into percentages, R interpret 30000 as 30000%, so if a limit to 100% I don't get anything in my graph. How can I manage it?
我有一个像这样的数据集:
I have a dataset like this:
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
可以使用以下代码重现:
which is reproducible with this code:
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轴变量.
so I don't have a variable for y axes I can manage.
这里是我的代码和获得的剧情
Here my code and the plot I obtained
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")
有人可以帮我吗?
我使用真实数据得到的结果
What I get on real data using
scale_y_continuous(label = scales::percent_format(scale = 100 / n_id))
是这样的:
,最大值为84%(而不是100%).我怎样才能使y轴达到100%并每10%断裂一次?
with 84% as the maximum value (and not 100%). How can i get the y-axes up to 100% and broken every 10% ?
这就是我所得到的
scale_y_continuous(breaks = scales::pretty_breaks(10), label = scales::percent_format(scale = 100 / n_id))
我每14%都会得到一个奇怪的值.
I get this weird values every 14%.
推荐答案
使用percent_format
中的scale
自变量可以像这样实现:
Using the scale
argument in percent_format
this can be achieved like so:
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 sup>
Created on 2020-05-19 by the reprex package (v0.3.0)
这篇关于如何获得冲积或sankey图中y轴上的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!