如何绘制R中每个染色体读数的密度直方图

时间:2020-12-27 15:00:31

I have this data file here https://www.dropbox.com/s/hei2fa25r9ezvkx/myfile.csv?dl=0

我这里有这个数据文件https://www.dropbox.com/s/hei2fa25r9ezvkx/myfile.csv?dl=0

I want to plot a density plot for chr1 to chr16 start position. So I want to partition the X axes into chr1, chr2, chr3, ..chr16 and within each chr partition, I want to get the density plot for each start position. I want the density variable plotted on the Y axis. I have tried something like this mentioned here, but it does not divide the Y axes in each chr group and their respective start position in X axis. Can someone please help me plot this. Thanks!

我想绘制chr1到chr16起始位置的密度图。所以我想将X轴分成chr1,chr2,chr3,.. chr16,并且在每个chr分区内,我想得到每个起始位置的密度图。我想在Y轴上绘制密度变量。我尝试过这里提到的类似的东西,但它并没有将每个chr组中的Y轴和它们各自的起始位置划分为X轴。有人可以帮我策划这个。谢谢!

args = commandArgs(trailingOnly=TRUE)
inFn = args[1]
outFn = args[2]
title = args[3]
xRange <- range(0, d$stop) 
yRange <- range(0, d$density)
pdf(outFn)
plot(xRange, yRange, type="l", xlab="Position", ylab="Density", main=title)
lines(d$start, d$density)
dev.off()

Updated with the reproducible data:

更新了可重现的数据:

d<- structure(list(chr = c("chr10", "chr10", "chr10", "chr10", "chr1", 
"chr1", "chr1", "chr1", "chr1", "chr1", "chr3", "chr3", "chr3", 
"chr3", "chr3", "chr3"), start = c(6, 14, 19, 24, 8442, 8446, 
8487, 8489, 8491, 8497, 13282, 13324, 13325, 13328, 13366, 13368
), stop = c(6, 14, 19, 24, 8442, 8446, 8487, 8489, 8491, 8497, 
13282, 13324, 13325, 13328, 13366, 13368), density = c(10, 4, 
1, 3, 9, 62, 43, 115, 419, 2, 982, 57, 14, 248, 16, 1)), .Names = c("chr", 
"start", "stop", "density"), row.names = c(1L, 2L, 3L, 4L, 6000L, 
6001L, 6002L, 6003L, 6004L, 6005L, 10000L, 10001L, 10002L, 10003L, 
10004L, 10005L), class = "data.frame")

1 个解决方案

#1


2  

From time to time I have to plot data like this and I always use geom_point. geom_bar is too stacked and geom_line looks bad if there are gaps in the chromosome (ie., centromere).

我不时要绘制这样的数据,我总是使用geom_point。如果染色体中存在间隙(即,着丝粒),则geom_bar过于堆叠并且geom_line看起来很糟糕。

You can try this:

你可以试试这个:

library(ggplot2)
ggplot(d, aes(start, density)) +
    geom_point(alpha = 0.9) +
    facet_grid(. ~ chr) +
    labs(title = "Density profiles along the chromosomes",
         x = "Coordinate, bp",
         y = "Density") +
    theme_bw()

You can use facet_grid(. ~ chr, scales = "free") which "zooms" into a specific region in each chromosome.

您可以使用facet_grid(.~chr,scales =“free”)“缩放”到每个染色体中的特定区域。

如何绘制R中每个染色体读数的密度直方图

#1


2  

From time to time I have to plot data like this and I always use geom_point. geom_bar is too stacked and geom_line looks bad if there are gaps in the chromosome (ie., centromere).

我不时要绘制这样的数据,我总是使用geom_point。如果染色体中存在间隙(即,着丝粒),则geom_bar过于堆叠并且geom_line看起来很糟糕。

You can try this:

你可以试试这个:

library(ggplot2)
ggplot(d, aes(start, density)) +
    geom_point(alpha = 0.9) +
    facet_grid(. ~ chr) +
    labs(title = "Density profiles along the chromosomes",
         x = "Coordinate, bp",
         y = "Density") +
    theme_bw()

You can use facet_grid(. ~ chr, scales = "free") which "zooms" into a specific region in each chromosome.

您可以使用facet_grid(.~chr,scales =“free”)“缩放”到每个染色体中的特定区域。

如何绘制R中每个染色体读数的密度直方图