how to show percentage on y axis properly for a bar plot with multiple variables?

Hello,

I have this bar plot, where percentages on y axis are not aligned to what bars show - probably because they are computed for data undivided by a variable in "fill" parameter.
What can be done to make them appear properly?
The code may be ugly - I based it on the bits found on the internet...

library(tidyverse)

ggplot(mtcars)+
  aes(x = rev(factor(cyl)), y=(..count..)/sum(..count..), fill = factor(vs))+
  geom_bar(stat="count", position = "dodge")+
  scale_fill_manual(values = c("0" = "darkgrey", "1" = "black"), labels=c("noVS","VS"), guide = guide_legend(reverse = TRUE))+
  geom_text(aes(label=paste0(after_stat(round(prop * 100,1)),"%"),group=vs), stat='count', hjust = 0, vjust = 0, position = position_dodge(width = 1))+
  labs(y=NULL, x=NULL, fill=NULL)+
  scale_x_discrete(labels=NULL)+
  scale_y_continuous(labels=scales::percent, limits = c(0,0.40))+
  coord_flip()+
  theme_bw()+
  theme(legend.position = c(.9,.2))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.