使用stat = summary在堆积条中添加文本,其中级别按顺序排列

时间:2016-08-27 20:50:03

标签: r ggplot2 geom-text

以下是数据:

df_test<-structure(list(MIRNA = c("let-7c", "let-7c", "let-7c", "let-7c", 
"let-7c", "let-7c", "let-7c", "mir-125b-2", "mir-125b-2", "mir-125b-2", 
"mir-125b-2", "mir-125b-2", "mir-125b-2", "mir-125b-2", "mir-155", 
"mir-155", "mir-155", "mir-155", "mir-155", "mir-155", "mir-155", 
"mir-4760", "mir-4760", "mir-4760", "mir-4760", "mir-4760", "mir-4760", 
"mir-4760", "mir-548x", "mir-548x", "mir-548x", "mir-548x", "mir-548x", 
"mir-548x", "mir-6501", "mir-6501", "mir-6501", "mir-6501", "mir-6501", 
"mir-6501", "mir-6501", "mir-6508", "mir-6508", "mir-6508", "mir-6508", 
"mir-6508", "mir-6508", "mir-6508", "mir-6814", "mir-6814", "mir-6814", 
"mir-6814", "mir-6814", "mir-6814", "mir-6815", "mir-6815", "mir-6815", 
"mir-6815", "mir-6815", "mir-6815", "mir-99a", "mir-99a", "mir-99a", 
"mir-99a", "mir-99a", "mir-99a", "mir-99a"), MIRNA_Feature = structure(c(6L, 
5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 
2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 
1L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 
5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 
1L, 4L), .Label = c("Precursor_5PrimeEnd", "5p_Seed", "5p_Mature", 
"Precursor_Loop", "3p_Seed", "3p_Mature", "Precursor_3PrimeEnd"
), class = "factor"), domain_length = c(13L, 9L, 13L, 9L, 7L, 
10L, 23L, 13L, 9L, 13L, 9L, 14L, 16L, 15L, 13L, 9L, 14L, 9L, 
1L, 3L, 16L, 13L, 9L, 13L, 9L, 7L, 9L, 20L, 14L, 9L, 11L, 9L, 
10L, 45L, 14L, 9L, 13L, 9L, 1L, 2L, 19L, 13L, 9L, 12L, 9L, 2L, 
4L, 11L, 13L, 9L, 13L, 9L, 5L, 21L, 12L, 9L, 14L, 9L, 5L, 12L, 
13L, 9L, 13L, 9L, 10L, 12L, 15L), order = c(6L, 5L, 3L, 2L, 7L, 
1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 
6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 6L, 5L, 3L, 
2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L, 6L, 5L, 3L, 2L, 1L, 
4L, 6L, 5L, 3L, 2L, 1L, 4L, 6L, 5L, 3L, 2L, 7L, 1L, 4L), expr = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.6, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 3.4, 1, 1, 3.6, 2.6, 1, 1, 1, 1, 1, 
2.4, 1, 1, 6, 3.4, 1, 1, 1, 1, 1, 1, 2.4, 1, 1, 1, 1, 2.8, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1.6)), .Names = c("MIRNA", "MIRNA_Feature", 
"domain_length", "order", "expr"), row.names = c("29", "30", 
"31", "32", "33", "34", "35", "334", "335", "336", "337", "338", 
"339", "340", "695", "696", "697", "698", "699", "700", "701", 
"3084", "3085", "3086", "3087", "3088", "3089", "3090", "4111", 
"4112", "4113", "4114", "4115", "4116", "4433", "4434", "4435", 
"4436", "4437", "4438", "4439", "4481", "4482", "4483", "4484", 
"4485", "4486", "4487", "5260", "5261", "5262", "5263", "5264", 
"5265", "5266", "5267", "5268", "5269", "5270", "5271", "6098", 
"6099", "6100", "6101", "6102", "6103", "6104"), class = "data.frame")

我根据MIRNA_Feature列中的级别对它们进行排序,如下所示:

df_test$MIRNA_Feature<-factor(df_test$MIRNA_Feature,levels=c("Precursor_5PrimeEnd","5p_Seed","5p_Mature","Precursor_Loop","3p_Seed","3p_Mature","Precursor_3PrimeEnd"))

然后用标签绘制堆积的条形图并获得此图:

ggplot(df_test,aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature))+geom_bar(stat="identity")+geom_label(aes(label=expr),position="stack")+coord_flip()

enter image description here

问题是我指定的订单丢失了。我可以使用stat = summary获得所需的订单,但是标签的顺序全部关闭:

ggplot(df_test,aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature))+geom_bar(stat="summary",fun.y=sum)+geom_label(aes(label=expr),position="stack")+coord_flip() 

情节如下: enter image description here

看起来标签的排序是单独完成的(按字母顺序排列),并且在使用stat =“summary”时,堆栈是按照指定的顺序完成的。任何帮助解决这个问题都非常感谢。

1 个答案:

答案 0 :(得分:2)

ggplot version 2.1, stat_summary中保留数据框中的顺序,因此您需要事先对值进行排序。例如:

ggplot(df_test[order(df_test$MIRNA, df_test$MIRNA_Feature),],
       aes(x=MIRNA,y=domain_length,fill = MIRNA_Feature)) + 
  geom_bar(stat="summary", fun.y=sum) +
  geom_label(aes(label=expr), position="stack") +
  coord_flip()

enter image description here

将标签置于每个条形中心会使绘图更容易混淆。您可以通过创建新列(我称之为y.pos)来设置标签位置。我已使用%>%包中的链接运算符(dplyr)来简化此操作:

library(dplyr)

df_test %>% 
  group_by(MIRNA) %>% 
  arrange(MIRNA, MIRNA_Feature) %>%
  mutate(y.pos = cumsum(domain_length) - 0.5*domain_length) %>%
ggplot(aes(x=MIRNA, fill = MIRNA_Feature)) + 
  geom_bar(stat="summary", aes(y=domain_length), fun.y=sum) +
  geom_label(aes(label=expr, y=y.pos), size=2.7, 
             label.padding=unit(0.15, "lines"), show.legend=FALSE) +
  coord_flip()

enter image description here