在使用 stat=summary 对级别排序的堆叠条中添加文本
Adding text in stacked bar where levels are ordered using stat=summary
这是数据:
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()
问题是我指定的顺序 got lost.I 可以像这样使用 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" 时指定的顺序完成的。非常感谢任何解决此问题的帮助。
In 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()
将每个条形中的标签居中会使情节不那么混乱。您可以通过创建一个新列(我称之为 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()
这是数据:
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()
问题是我指定的顺序 got lost.I 可以像这样使用 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" 时指定的顺序完成的。非常感谢任何解决此问题的帮助。
In 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()
将每个条形中的标签居中会使情节不那么混乱。您可以通过创建一个新列(我称之为 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()