I'm trying to make a stacked bar chart with text labels, this some example data / code:
library(reshape2)
ConstitutiveHet <- c(7,13)
Enhancer <- c(12,6)
FacultativeHet <- c(25,39)
LowConfidence <- c(3,4)
Promoter <- c(5,4)
Quiescent <- c(69,59)
RegPermissive <- c(23,18)
Transcribed <- c(12,11)
Bivalent <- c(6,22)
group <- c("all","GWS")
meanComb <- data.frame(ConstitutiveHet,Enhancer,LowConfidence,Promoter,Quiescent,RegPermissive,Transcribed,Bivalent,group)
meanCombM <- melt(meanComb,id.vars = "group")
ggplot(meanCombM,aes(group,value,label=value)) +
geom_col(aes(fill=variable))+
geom_text(position = "stack")+
coord_flip()
The text labels appear out of order, they seem to be the mirror image of their intended order. (you get the same problem with or without the coord_flip()
)
A poster had a similar problem here: ggplot2: add ordered category labels to stacked bar chart
An answer to their post propsed reversing the order of the values in the groups, which I tried (see below), the resulting order on the plot is not one i've been able to figure out. Also this approach seems hacky, is there a bug here or am I missing something?
x <- c(rev(meanCombM[meanCombM$group=="GWS",]$value),rev(meanCombM[meanCombM$group=="all",]$value))
ggplot(meanCombM,aes(group,value,label=x)) +
geom_col(aes(fill=variable))+
geom_text(position = "stack")+
coord_flip()