gpt4 book ai didi

r - 在条形图中添加每组的观察数(ggplot2)

转载 作者:行者123 更新时间:2023-12-04 10:30:09 25 4
gpt4 key购买 nike

我正在尝试将观察次数添加到我的情节中,但我不知道我是否正在接近这一点。
我使用 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
))

绘图示例

enter image description here

最佳答案

由于每个间隔只有一个值可绘制为标签,因此您可以使用 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))
enter image description here
它回答你的问题吗?

关于r - 在条形图中添加每组的观察数(ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60453389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com