ggplot2:将已排序的类别标签添加到堆积条形图

时间:2017-06-02 17:36:29

标签: r ggplot2

我试图制作微生物组丰度的堆积条形图(最后是数据集)。由于有很多课程,很难区分颜色,所以我想在栏上放置每个类别的文字标签(我知道还有其他关于添加数据标签的问题,但我无法&# 39;找不到类别文本的内容。)

以下ggplot来电使用:

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

但这是它所提供的图表:

Chart with backwards labels

据我所知,问题是geom_text标签的添加顺序不正确。如何在条形的相应部分上获得正确的标签?

编辑:使用 geom_text(label=c(rev(levels(abun$Order)),rev(levels(abun$Order))),position=position_stack(vjust=0.5)) 解决它,但这是非常不优雅的。有更好的解决方法吗?

数据集:

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

3 个答案:

答案 0 :(得分:0)

这有用吗?

ggplot(abun, aes(x = status, y = freq, fill = Order, label = Order)) +
  geom_bar(stat = "identity", col = "black") +
  ylab("Frequency (%)") +
  geom_text(position = position_stack(vjust = 0.5)) +
  theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))

答案 1 :(得分:0)

更改了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))+
theme(text=element_text(size=20,face="bold"),legend.text=element_text(size=12,face="plain"))

得到以下特性:

enter image description here

似乎geom_text在不考虑x的情况下循环所有因素级别。修复此问题需要两次运行反向列表:label=c(rev(levels(abun$Order)),rev(levels(abun$Order)))

因此,对于x映射下的n个因子级别,请包括c(rev(levels(LabelText))) n次。

感谢@Lyngbakr让我走上正轨。

答案 2 :(得分:0)

正如@Lyngbakr建议您可以使用主要情节函数或label中的geom_text审美来解决此问题。

ggplot(abun, aes(x = status, y = freq, fill = Order)) +
    geom_bar(stat = "identity", col = "black") +
    ylab("Frequency (%)") +
    geom_text(aes(label = Order), position = position_stack(vjust = 0.5)) +
    theme(text = element_text(size = 20, face = "bold"), legend.text = element_text(size = 12, face = "plain"))

这是因为geom_text中的默认分组(请参阅: http://ggplot2.tidyverse.org/reference/aes_group_order.html)在这里不起作用,需要用美学来指定。

我自己也有类似的问题:Out of order text labels on stack bar plot (ggplot)

相关问题