堆栈条图上的乱序文本标签(ggplot) [英] Out of order text labels on stack bar plot (ggplot)
问题描述
我试图制作带有文本标签的堆叠条形图,这是一些示例数据/代码:
library( (7,3)
增强器<-c(12,6)
FacultativeHet <-c(25,39)
LowConfidence <-c(3,4)
Promoter <-c(5,4)
Quiescent <-c(69,59)
RegPermissive <-c( 23,18)
转录< -c(12,11)
Bivalent <-c(6,22)
组<-c(全部,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()
)
海报也有类似的问题:
ggplot2:将有序的类别标签添加到堆叠条形图
对他们的帖子的回答提出了颠倒组合中的值的顺序,我试过了(见下文),图上的结果顺序是没有一个我能弄明白。
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()
$ c $
= 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 )在没有指定组的情况下,不会正确分区数据
审美,在本例中应该映射到与 fill
在 geom_col
中相同的值。
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()
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 answered a question similar to my own in this issue in ggplot2's git repository: https://github.com/tidyverse/ggplot2/issues/1972
Apparently the default grouping behaviour (see:
http://ggplot2.tidyverse.org/reference/aes_group_order.html) does not partition the data correctly here without specifying a group
aesthetic, which should map to the same value as fill
in geom_col
in this example.
这篇关于堆栈条图上的乱序文本标签(ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!