简体   繁体   中英

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 | 20
60 | 120
178 | 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. It's something like this:

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

That kind of chart is called "Gantt", here's a possible way to draw it in base 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 :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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