I want to make a simple barplot, I have one variable x (A,B,C,D) categorical, another one y (YES, NO) that I am using to fill, and a set of observations, and I want to display a filled barplot, with percentage labels in each column.
Something as simple as like this:
So far ggplot layer system has been a nightmare to use. And no solutions I was able to find in already asked questions.
x11()
ggplot(data=KS, aes(x=KS$main_category, fill=KS$state)) +
geom_bar(position="fill") +
scale_y_continuous(labels = percent) +
geom_text(aes(label = ..count.., group = KS$state),
stat = "count")
This is what I got so far and a part for positioning it displays the count for every category and state, why can't it display proportions?. And I want to avoid to manipulate the data and adding stuff to the dataframe.
Thanks a lot.
Edit: the requested data frame
library("ggplot2")
library("scales")
main_category=c('A','A','B','C','D','A','A','B','C','D','A','A','B','C','D','A','A','B','C','D')
state=c('Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes','Yes', 'No', 'Yes', 'Yes', 'No', 'No', 'No', 'No')
KS = data.frame(main_category, state)
Edit 2:
I was able to find my own solution without manipulating the dataset by using implicit ggplot variables:
geom_text(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..], label=percent(..count../tapply(..count.., ..x.. ,sum)[..x..]) ),
stat="count", position=position_fill(0.5), vjust=0.5)