在R中创建复杂的线图

时间:2015-02-27 20:56:09

标签: r plot bioinformatics

我正在尝试在R中为我的CNV数据制作一个线图。我的.csv文件格式如下:

    Chromosome     Start       End Call
1         chr1     14620   1577873    2
2         chr1   1595921   1641083    1
3         chr1   1823218   3542170    1
4         chr1   3542242   6695502    1

我的目标是在y轴上Call,在x轴上StartEnd。我希望这是一个线图(在一个CNV以及其他CNV的StartEnd之间创建一条线)。

我知道如何制作一个基本的情节(x,y),但我无法弄清楚如何制作一个将起点和终点作为不同点然后用线连接它们的情节。

感谢任何指导!

1 个答案:

答案 0 :(得分:2)

正如Alexis评论的那样,可以使用segments绘制CNV数据。您可以使用以下脚本作为起点:

<强> cnv_plot.R

file="cnv_call.txt"
dataTable <-read.table(file, header=TRUE);
ratio<-data.frame(dataTable)
ploidy <- 2 # amplification > 2, deletion < 2 

##
png(filename = paste(file,".png",sep = ""), width = 1080, height = 1080,
units = "px", pointsize = 20, bg = "white", res = NA)
plot(1:10)
op <- par(mfrow = c(5,5))
##

chrom = unique(ratio$Chromosome)
for (i in (chrom)) {
    region <- which(ratio$Chromosome==i)

   #png(filename = paste(file,".",i,".png",sep = ""), width = 640, height = 480,
   #units = "px", pointsize = 20, bg = "white", res = NA)

   if (length(region)>0) {
    plot(ratio$Start[region],ratio$Call[region],xlim = c(0,max(ratio$End[region])),ylim = c(0,max(ratio$Call)),xlab = paste ("position, ",i),ylab = "CNV",pch = ".",col = "black")
    region <- which(ratio$Chromosome==i  & ratio$Call>ploidy )
    segments(ratio$Start[region],ratio$Call[region],ratio$End[region],ratio$Call[region],col = "red",lwd=2)
    region <- which(ratio$Chromosome==i  & ratio$Call<ploidy )
    segments(ratio$Start[region],ratio$Call[region],ratio$End[region],ratio$Call[region],col = "blue",lwd=2)
    region <- which(ratio$Chromosome==i  & ratio$Call==ploidy)
    segments(ratio$Start[region],ratio$Call[region],ratio$End[region],ratio$Call[region],col = "darkgreen",lwd=2)
   }
   #dev.off()
}
##
dev.off()
##

enter image description here