[英]Add a number of observations per group AND SUBGROUP in ggplot2 boxplot
[英]Add number of observations per group in barplot (ggplot2)
我正在尝试将观察次数添加到我的情节中,但我不知道我是否正在接近这一点。 我使用 ggplot2 和 tidyverse 包绘制了我的图。
我想要的是在图表的每一列的顶部添加观察的数量(在数据的 Obs 列中)。 但是,看起来我做得不对,因为它采用了列的每个部分的值。
有谁知道如何解决这个问题? 非常感谢!
图的代码
Euk_one %>%
drop_na(`Length Stretch`) %>%
mutate_at(c("GGA","GGG","GGT","GGC"),~.x/`Length Stretch`) %>%
group_by(interval) %>% select(c(interval, "GGA","GGG","GGT","GGC", Obs))%>%
summarize_all(mean) %>%pivot_longer(-c(interval, Obs)) %>%
ggplot(aes(x=interval, y=value, fill=name)) + geom_col() +
ggtitle("Eukarya 1 Codon Stretches - Distribution of codons ")+
labs(fill= "Codons") +
theme(axis.text.x = element_text(angle = 45)) + geom_text(aes(label=Obs), vjust = -.25)
示例数据
structure(list(interval = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("[6,7]",
"(7,8]", "(8,9]", "(9,10]"), class = "factor"), `Length Stretch` = c(8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10,
10, 10, 10, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 7,
7, 6, 6, 7, 6, 6, 7, 7, 7, 6), GGA = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 7, 0, 6, 0, 0, 0, 0, 0, 7, 6, 6, 0, 0, 0,
0), GGG = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), GGT = c(8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10,
6, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 0, 6, 0, 6, 7, 7, 6, 6, 0,
0, 0, 7, 7, 7, 6), GGC = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), `Length Gene` = c(5250,
5250, 3021, 1905, 2910, 2973, 2367, 3021, 2931, 2754, 2424, 2862,
2436, 5061, 4935, 23949, 23988, 24024, 2733, 2733, 2358, 2358,
2733, 2733, 2838, 2079, 2100, 1998, 2835, 1197, 1197, 2838, 1998,
3795, 3795, 3795, 1320, 1827, 2088, 663, 10395, 10545, 2340,
2340, 1323, 1977, 2085, 4773, 10557, 10554, 1920), NumGens = c(19297,
19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297,
19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297,
19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297,
19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297,
19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297, 19297,
19297, 19297, 19297, 19297, 19297), Count = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), Obs = c(13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L)), class = "data.frame", row.names = c(NA, -51L
))
情节示例
由于每个间隔只有一个值可绘制为标签,因此您可以使用dplyr
distinct
函数过滤数据dplyr
并设置单个 y 值来绘制它:
library(tidyverse)
Euk_one %>%
drop_na(`Length Stretch`) %>%
mutate_at(c("GGA","GGG","GGT","GGC"),~.x/`Length Stretch`) %>%
group_by(interval) %>% dplyr::select(c(interval, "GGA","GGG","GGT","GGC", Obs))%>%
summarize_all(mean) %>%pivot_longer(-c(interval, Obs)) %>%
ggplot(aes(x=interval, y=value, fill=name)) + geom_col() +
ggtitle("Eukarya 1 Codon Stretches - Distribution of codons ")+
labs(fill= "Codons") +
theme(axis.text.x = element_text(angle = 45)) +
geom_text(inherit.aes = FALSE, data = . %>% distinct(interval, Obs),
aes(x = interval, y = 0.25, label=Obs))
它回答你的问题吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.