0

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:

https://i.stack.imgur.com/aHXLD.jpg

alexvisio
  • 3
  • 2
  • 1
    That chart is called "Gantt". Have a look at [this q&a](http://stackoverflow.com/questions/9862519/gantt-style-time-line-plot-in-base-r) – digEmAll Sep 09 '14 at 12:10

1 Answers1

0

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 :

enter image description here

digEmAll
  • 56,430
  • 9
  • 115
  • 140