I am trying to add percentage labels to a stacked AND faceted barplot (position='fill'). I want the percentages displayed to add up for each bar.
I'm using a data set like this:
## recreate dataset
Village<-c(rep('Vil1',10),rep('Vil2',10))
livestock<-c('p','p','p','c','c','s','s','s','g','g',
'p','p','c','c','s','s','s','s','g','g')
dose<-c(3,2,1,2,1,3,2,1,2,1,
2,1,2,1,4,3,2,1,2,1)
Freq<-c(4,5,5,2,3,4,1,1,6,8,
1,3,2,2,1,1,3,2,1,1)
df<-data.frame(Village,livestock,dose,Freq)
I sucessfully plotted it and added labels that add up to 100% for each X variable (livestock):
## create dose categories (factors)
df$dose<-as.character(df$dose)
df$dose[as.numeric(df$dose)>3]<-'>3'
df$dose<-factor(df$dose,levels=c('1','2','3','>3'))
## percentage barplot
ggplot(data = df, aes(x=livestock, y=Freq, fill=dose)) +
geom_bar(position='fill', stat='identity') +
labs(title="Given doses of different drugs in last 6months (livestock)",
subtitle='n=89',x="Livestock",y="Percentage",
fill = "Nr. of\ndoses") +
theme(axis.text.x = element_text(angle = 45, hjust=1))+
scale_y_continuous(labels=percent)+
facet_wrap(~Village)+
geom_text(aes(label = percent(..y../tapply(..y..,..x..,sum)[..x..])),
stat = "identity",position = position_fill(vjust=0.5))
Does anyone know how I can change the label code within ggplot so the percentages add up to 100% for each bar? Maybe something to do with ..group..?
I tried something similar to this: Label percentage in faceted filled barplot in ggplot2 put I can't make it work for my data.