this is how my data is:

As you can see each ID has 10 observations with a certain "status" at each time point between 0-10 for each ID.

       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

So, this was how my initial plot command was

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))

Then I wanted to convert the Y-axis into percentage plot, so my command was changed to:

axs + scale_y_continuous(labels=function(x){round(x*100/length(unique(dat$id)))})

So then I modified it as:

axs + scale_y_continuous(labels=function(x){round(x*100/length(unique(dat$id)))}, breaks=c(0,100,25))

The same figure is what I get when I type break= seq(0,100,25)). When I use the breaks in the scale_y_continuous command, I get only 0 on the Y-axis. If I dont use it, then I am getting 101 in the Y-axis, which I am trying to eliminate (because its percentage on the X-axis).

Can someone tell me how I can sort out this problem? Would greatly appreciate it, Thanks, Shalom

1

There are 1 answers

9
Duck On BEST ANSWER

Try this:

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))

Output:

enter image description here