I am trying to plot two factor variables and label the results with % inside the plots.
I already checked this post and the links he/she provides :
How to center stacked percent barchart labels
The ggplot line you are seing here is actually from one of the posts recommended :
sex <- c("F","F","M", "M", "M", "F","M","F","F", "M", "M", "M", "M","F","F", "M", "M", "F")
behavior <- c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "B", "C", "A")
BehSex <- data.frame(sex, behavior)
ggplot(BehSex, aes(x= factor(sex), fill= factor(behavior), y = (..count..)/sum(..count..)))+
geom_bar() +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = 5)
However, when I use that line I get the following error :
Error: StatBin requires a continuous x variable: the x variable is discrete. Perhaps you want stat="count"?
I tried using stat="count" inside the geom_bar() but it doesn't seem to work as expected.
Three questions:
1) What am I doing wrong?
2) How can I manage to plot what I want?
3) How can I plot: the % and then in another graph the counts?
Here's the plot that I have right now
Thank you in advance for your help!