R ggplot2 - geom_histogram: levels / color removed from the plot due to y-scale constraint

For each year, I have the number of an individual patient that belongs to one of three levels. I would like to calculate the relative frequency distribution of the three levels for each year. Say that 80% of patients are labeled with C and another patient with A and B. Since most have C, the distribution for A and B will not be visible. So, I changed the y axis. I had the following problem with ggplot: the color column for A and B is displayed, but for C it disappeared from the graph. Here I gave an example:

library(ggplot2) # Data set grp <- rep(c("A","B","C"), c(10,10,80)) year <- floor(runif(100)*10/3) df <- data.frame(grp,year) # Plot ggplot(df,aes(year)) + geom_histogram(aes(fill=grp),position="fill") + scale_y_continuous(lim=c(0,0.5)) 

If I delete the last line (scale_y ...), I get the whole range from 0 to 1 and all levels (colors) are displayed. With scale_y .. the level (color) of C disappears and only a gray background is visible. Does anyone know how I can avoid the color for C fading? Thanks for the tips.

+4
source share
1 answer

As @Harpal already noted, when you set limits inside scale_y_continuous() , all values โ€‹โ€‹that go beyond this limit are removed from the chart. If you need to "increase" your chart to values โ€‹โ€‹from 0 to 0.5, use coord_cartesian() instead of scale_y_continuous() .

 ggplot(df,aes(year)) + geom_histogram(aes(fill=grp),position="fill") + coord_cartesian(y=c(0,0.5)) 

enter image description here

+7
source

Source: https://habr.com/ru/post/1487266/


All Articles