Percent values on stacked geom_bar()

Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Percent values on stacked geom_bar()

atheeralattar
I have the dataset:

Gender Success
1  Girls  1
2   Boys  2
3   Boys  2
4   Boys  1
5   Boys  1
6   Boys  1
7  Girls  1
8   Boys  1
9   Boys  2
10  Boys  1

I want to add the partial percent values on each portion on the stacked geom_bar plot below:



This is the code I am using to generate the plot



Gender<-sample(c("Boys","Girls"),10,replace = T)
success<-sample(c(1:2),10,replace = T)
df<-data.frame(Gender,success)
ggplot(data=df, aes(x=Gender,fill=as.factor(success))) +geom_bar(stat="count", position = "fill")+xlab("Gender")+ylab("Count")+    labs(fill = "Success/Fail")+scale_fill_discrete(name="Pass/Fail", labels=c("Pass","Fail"))

Please note that we are coding 1 for Pass and 2 for Fail.