问题描述
这是我的数据:
如您所见,每个ID在每个ID的0到10之间的每个时间点都有10个观测值,并带有一定的“状态”。
id year status
1 20 0 2
2 20 1 2
3 20 2 2
4 20 3 5
5 20 4 5
6 20 5 5
7 20 6 5
8 20 7 5
9 20 8 5
10 20 9 5
11 20 10 5
12 35 0 2
13 35 1 5
14 35 2 5
15 35 3 5
16 35 4 5
17 35 5 5
18 35 6 5
19 35 7 5
20 35 8 5
所以,这就是我最初的绘图命令的方式
axs=dat %>% group_by(year)%>% ggplot(aes(x=year,fill = factor(status))) +
+ geom_bar(position = "stack",width = 1) + stat_count()+
+ scale_fill_hue(labels = c("Not listed","Listed","Removed","Given","Wasted")) +
+ labs(title= "Trajectory ",x = "time",y = "Percent ",fill = "Status") + scale_x_continuous(breaks = seq(0,10,1))
然后我想将Y轴转换为百分比图,所以我的命令更改为:
axs + scale_y_continuous(labels=function(x){round(x*100/length(unique(dat$id)))})
所以我将其修改为:
axs + scale_y_continuous(labels=function(x){round(x*100/length(unique(dat$id)))},breaks=c(0,100,25))
当输入break = seq(0,25))时,我得到的是相同的数字。 当我在scale_y_continuous命令中使用中断时,在Y轴上仅获得0。如果我不使用它,那么我将在Y轴上获得101,我试图将其消除(因为它在X轴上的百分比)。
有人可以告诉我如何解决这个问题吗? 非常感谢 谢谢, 沙洛姆
解决方法
尝试一下:
axs=dat %>% group_by(year)%>% ggplot(aes(x=year,fill = factor(status))) +
geom_bar(position = "stack",width = 1) + stat_count()+
scale_fill_hue(labels = c("Not listed","Listed","Removed","Transplanted","Died")) +
labs(title= "Trajectory in less than 65 yrs age",x = "Years after graft failure",y = "Percent of patients",fill = "Status") +
scale_y_continuous(labels = function(x) paste0(100*x/max(x),'%'))+
scale_x_continuous(breaks = seq(0,10,1))
输出: