我想沿着不同的样本绘制基因组学区域每个碱基的平均覆盖率:我想绘制每个基因组在x轴上的位置,以及它在y轴上的平均覆盖率。
我有这个文件:其中第一列表示基因组区域,第二列表示不同样本的平均覆盖率。
12:36761802-36761804 54
12:36761802-36761805 42
12:36761802-36761806 56.5
12:36761802-36761810 28.75
12:36761803-36761804 29
12:36761803-36761806 30正如您所看到的,行是重叠的,所以我不确定如何绘制它。
发布于 2020-03-23 18:24:57
您可以将范围绘制为线段:
library(ggplot2)
df$region_start <- as.numeric(unlist(lapply(strsplit(df$region, "-"), `[`, 1)))
df$region_end <- as.numeric(unlist(lapply(strsplit(df$region, "-"), `[`, 2)))
ggplot(df, aes(x = region_start, y = mean, colour = region)) +
geom_segment(aes(xend = region_end, yend = mean), size = 1.5) +
labs(x = "locus")

编辑
在从OP中进一步澄清之后,它是应该绘制的每个点的值的平均值。这稍微有点复杂,但使用dplyr只需几行代码即可完成
library(ggplot2)
library(dplyr)
df$region_start <- as.numeric(unlist(lapply(strsplit(df$region, "-"), `[`, 1)))
df$region_end <- as.numeric(unlist(lapply(strsplit(df$region, "-"), `[`, 2)))
do.call("rbind", mapply(function(x, y, z) cbind(x:y, rep(z, 1 + y - x)),
df$region_start,
df$region_end,
df$mean)) %>%
as.data.frame() %>%
setNames(c("Locus", "Value")) %>%
group_by(Locus) %>%
summarise(mean = mean(Value)) %>%
ggplot(aes(x = Locus, y = mean)) +
geom_line() + labs(x = "Locus")

使用的数据:
region mean
36761802-36761804 54
36761802-36761805 42
36761802-36761806 56.5
36761802-36761810 28.75
36761803-36761804 29
36761803-36761806 30Reprex:
df <- structure(list(region = c("36761802-36761804", "36761802-36761805",
"36761802-36761806", "36761802-36761810", "36761803-36761804",
"36761803-36761806"), mean = c(54, 42, 56.5, 28.75, 29, 30)),
class = "data.frame", row.names = c(NA, -6L))https://stackoverflow.com/questions/60810638
复制相似问题