Home > Blockchain >  How to get percentage values in a stacked bar in ggplot?
How to get percentage values in a stacked bar in ggplot?

Time:10-06

at the moment this is what my graph looks like. enter image description here

Now everythings fine with that. I just want to get percentage values instead of count for the different categories (A-E). Here's my code.

g6.2 <- ggplot(data=dataKL, aes(x="", fill=ehz_new2)) 
geom_bar(width = 1) 
labs(y=element_blank(), x="2021", fill=element_blank(), subtitle = "c)") 
theme(plot.subtitle = element_text(hjust = 1), axis.text=element_text(size=10), axis.ticks.y = element_blank(),axis.title=element_text(size=12), panel.background = element_rect(fill = "white",linetype = "solid", color = "black" ), axis.text.y = element_blank()) 
geom_text(aes(label= ifelse(..count.. > 1, ..count.., NA)), stat="count", position=position_stack(0.5)) 
scale_fill_manual(element_blank(), values = c("A" = "green3", "B" = "yellow2", "C" = "orangered", "D" = "lightblue3", "E"="darkgrey"))

I sticked with the count for the moment cause I couldn't find a solution yet. I tried:

"geom_text(aes(label = scales::percent(..prop..), group=1)," but it did not work for the stacked bar.

Can anyone help me to get a solution inside ggplot?

P.S. my first post, so don't be to hard on me precision-wise ;)

CodePudding user response:

This question has already been answered here. Also, it would make it easier to address your specific issue if you provided reproducible data.

CodePudding user response:

Maybe use

 geom_col(position = "fill")
  • Related