堆栈条形图 (ggplot) 上乱序的文本标签

Out of order text labels on stack bar plot (ggplot)

我正在尝试制作带有文本标签的堆叠条形图,这是一些示例数据/代码:

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()

文本标签出现乱序,它们似乎是其预期顺序的镜像。 (有或没有 coord_flip() 都会遇到同样的问题)

一个poster在这里遇到了类似的问题:

对他们 post 的回答建议颠倒组中值的顺序,我试过了(见下文),结果在情节上的顺序不是我能够弄清楚的.而且这种方法看起来很老套,这里有错误还是我遗漏了什么?

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()
ggplot(meanCombM,aes(group,value,label=value)) +
     geom_col(aes(fill=variable))+
     geom_text(aes(group=variable),position = position_stack(vjust = 0.5))+
     coord_flip()

Hadley 在 ggplot2 的 git 存储库中回答了一个与我自己类似的问题:https://github.com/tidyverse/ggplot2/issues/1972

显然是默认的分组行为(参见: http://ggplot2.tidyverse.org/reference/aes_group_order.html) 在没有指定 group 美学的情况下不会在此处正确划分数据,在本例中应映射到与 geom_col 中的 fill 相同的值。