0

这是数据:

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

在此处输入图像描述

问题是我指定的顺序丢失了。我可以像这样使用 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() 

情节如下所示: 在此处输入图像描述

看起来标签的顺序是单独完成的(按字母顺序),并且在使用 stat="summary" 时,堆栈是根据指定的顺序完成的。非常感谢解决此问题的任何帮助。

4

1 回答 1

2

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

在此处输入图像描述

将每个条形中的标签居中会使绘图不那么混乱。您可以通过创建一个新列(我称之为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()

在此处输入图像描述

于 2016-08-27T21:08:08.260 回答