简体   繁体   English

R-将文件中的间隔绘制为2D矩阵

[英]R - Plot intervals in files as 2D matrix

In my problem there are subregions of a larger region that can be classified as positive or negative. 在我的问题中,存在较大区域的子区域,可以将其划分为正数或负数。 I have several files with different classifications, in the following format: 我有几个具有不同分类的文件,格式如下:

start | 开始 end 结束
10 | 10 | 20 20
60 | 60 | 120 120
178 | 178 | 220 220

They are sorted, and they have only positive subregions, the rest are assumed negative. 它们被排序,并且只有正的子区域,其余的则假定为负。

I would like to represent this data in a 2D graphic in R, but I don't know what type of graph I should use. 我想用R中的2D图形表示此数据,但是我不知道应该使用哪种图形。 It's something like this: 就像这样:

http://i.imgur.com/VaSvEKr.jpg http://i.imgur.com/VaSvEKr.jpg

That kind of chart is called "Gantt", here's a possible way to draw it in base R : 这种图表称为“甘特图”,这是在基数R中绘制它的一种可能方法:

# input example
DF <- 
read.csv(text=
'"file","start","end"
"file1",10,20
"file1",60,120
"file1",178,220
"file2",10,20
"file2",25,100
"file2",130,140
"file2",190,210
"file3",0,50
"file3",55,400',stringsAsFactors=F)


minval <- min(DF$start) # or different if you know the limits
maxval <- max(DF$end)   # or different if you know the limits

files <- rev(unique(DF$file))
nfiles <- length(files)

# empty plot to make space for everything
filehigh <- 1.0
plot(c(minval,maxval),c(filehigh/2,nfiles+filehigh/2),type='n', xlab='Time',ylab=NA,yaxt='n' )

# add y labels
axis(side=2,at=1:nfiles,labels=files,las=1)

# plot the rectangles
negcolor <- 'red'
poscolor <- 'green'

for(i in 1:nfiles){
   file <- files[i]
   subDF <- DF[DF$file == file,]
   lastend <- minval
   for(r in 1:nrow(subDF)){
     yTop <- i+(filehigh/2)
     yBottom <- i-(filehigh/2)
     start <- subDF[r,'start']
     end <- subDF[r,'end']

     if(start > lastend){
       rect(lastend,yBottom,start,yTop,col=negcolor )
     }
     rect(start,yBottom,end,yTop,col=poscolor)
     lastend <- end
   }
   if(lastend < maxval){
     rect(lastend,yBottom,maxval,yTop,col=negcolor )
   }
}

Result : 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM