ggplot2:添加有序的分类标签到堆积条形图 [英] ggplot2: add ordered category labels to stacked bar chart
问题描述
我试图制作一个微生物组丰富度的堆积条形图(最后的数据集)。由于有很多类,所以很难区分不同的颜色,所以我想把每个类别的文本标签放在条上(我知道还有其他关于添加数据标签的问题,但我找不到任何
以下是我使用的 ggplot
调用:
ggplot(abun,aes(x = status,y = freq,fill = Order))+
geom_bar(stat =identity,col =黑色)+
ylab(Frequency(%))+
geom_text(label = abun $ Order,position = position_stack(vjust = 0.5))+
theme(text = element_text legend_text = element_text(size = 12,face =plain))
但是,下面是它推出的图表:
据我所知,问题在于 geom_tex t
标签按不正确的顺序添加。
geom_text(label = c(rev)如何才能将正确的标签贴到条的适当部分上?
(水平(abun $ Order)),rev(水平(abun $ Order))),position = position_stack(vjust = 0.5))
修正了这个问题,但这令人惊讶地不起眼。数据集:
abun =结构(列表(顺序=结构(c(11L,5L,15L,1L,8L,7L,
12L,2L,6L,10L,3L,4L,14L,13L,9L,11L,5L,15L,1L ,8L,
7L,12L,2L,6L,10L,3L,4L,14L,13L,9L),标签= c(Actinomycetales,
Bacteroidales ,双歧杆菌,梭状杆菌,
肠杆菌,Fusobacteriales,乳杆菌,其他,
巴斯德氏菌,Pseudomonadales,SBla14,Synergistales b $ bTuricibacterales,Unidentified),class =factor),status = structure(c(2L,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L ,2L,2L,2L,2L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L) (23.45555556,20.22962963,
19.98888889,13.46296296,7.562962963,2.607407407,5.6,1.451851852,
0.196296296,1.4,b $ b2),class =factor ,0.651851852,0.018518519,0.6644444444,0.6681481481,
1.888888889,60.42,21.2,3 (订单,
状态,等等),等等。 freq),row.names = c(NA,-30L),class =data.frame)
因此更改了 geom_text
调用:
ggplot(abun,aes(x = status,y = freq,fill = Order))+
geom_bar(stat =identity,col =black)+
ylab(Frequency(%))+
geom_text(label = c(rev(levels(abun $ Order)),rev(levels(abun $ Order))),position = position_stack(vjust = 0.5)) +
主题(text = element_text(size = 20,face =bold),legend.text = element_text(size = 12,face =plain))
yielding:
看起来
geom_text
在所有因素水平周期循环,而不考虑x
。解决这个问题需要两次运行反向列表:label = c(rev(levels(abun $ Order)),rev(levels(abun $ Order)))
因此,对于
x
映射下的n个因子级别,包含c(rev(levels(LabelText) ))
n次。
感谢@Lyngbakr让我走上正轨。
I'm trying to make a stacked bar chart of microbiome abundance (dataset at the end). Since there are a lot of classes, it's hard to tell the difference between colors, so I want to put text labels for each category on the bars (I know there are other questions about adding data labels, but I couldn't find anything for category text).
Here's the
ggplot
call I'm using:ggplot(abun, aes(x = status, y = freq, fill = Order)) + geom_bar(stat = "identity", col = "black") + ylab("Frequency (%)") + geom_text(label = abun$Order, position = position_stack(vjust = 0.5)) + theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))
But here's the chart it puts out:
As far as I can tell, the problem is that the
geom_text
labels are being added in incorrect order. How can I get the right labels onto the appropriate segments of the bars?EDIT: Using
geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5))
fixes it, but that's surprisingly inelegant. Is there a better workaround?Dataset:
abun=structure(list(Order = structure(c(11L, 5L, 15L, 1L, 8L, 7L, 12L, 2L, 6L, 10L, 3L, 4L, 14L, 13L, 9L, 11L, 5L, 15L, 1L, 8L, 7L, 12L, 2L, 6L, 10L, 3L, 4L, 14L, 13L, 9L), .Label = c("Actinomycetales", "Bacteroidales", "BD7-3", "Bifidobacteriales", "Clostridiales", "Enterobacteriales", "Fusobacteriales", "Lactobacillales", "Other", "Pasteurellales", "Pseudomonadales", "SBla14", "Synergistales", "Turicibacterales", "Unidentified"), class = "factor"), status = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("one", "two"), class = "factor"), freq = c(23.45555556, 20.22962963, 19.98888889, 13.46296296, 7.562962963, 2.607407407, 5.6, 1.451851852, 0.196296296, 1.4, 0.651851852, 0.018518519, 0.644444444, 0.681481481, 1.888888889, 60.42, 21.2, 3.94, 0.2, 0.84, 3.44, 0.44, 3.36, 2.46, 0.22, 0.64, 1.14, 0.4, 0.14, 1.06)), .Names = c("Order", "status", "freq"), row.names = c(NA, -30L), class = "data.frame")
解决方案Changed the
geom_text
call thus:ggplot(abun,aes(x=status,y=freq,fill=Order))+ geom_bar(stat="identity",col="black")+ ylab("Frequency (%)")+ geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5))+ theme(text=element_text(size=20,face="bold"),legend.text=element_text(size=12,face="plain"))
yielding:
It seems
geom_text
cycles through all factor levels without regard forx
. Fixing this required running through the reversed list twice:label=c(rev(levels(abun$Order)),rev(levels(abun$Order)))
So for n factor levels under the
x
mapping, includec(rev(levels(LabelText)))
n times.Thanks to @Lyngbakr for getting me on the right track.
这篇关于ggplot2:添加有序的分类标签到堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!