Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.37 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Plotting a rather complex chart using R and axis break()
  2. set.seed(1)
  3.     # make data.frame just with info for the lines you'll actually draw
  4.     # your data was mostly zeros, no need for those lines
  5.     position <- sort(sample(1:4563,45,replace = FALSE))
  6.     # but the x position needs to be shaved down!
  7.     # modulars are the real x positions on the plot:
  8.     xpos <- position%%600
  9.     # line direction appeared in your example but not in your text
  10.     posorneg <- sample(c(-1,1),45,replace = TRUE,prob=c(.05,.95))
  11.     # oxidant concentration for line width- just rescale the oxidant concentration
  12.     # values you have to fall between say .5 and 3, or whatever is nice and visible
  13.     oxconc   <- (.5+runif(45))^2
  14.     # oxidant type determines line type- you mention 2
  15.     # just assign these types to lines types (integers in R)
  16.     oxitype  <- sample(c(1,2),45,replace = TRUE)
  17.     # let's say there's another dimension you want to map color to
  18.     # in your example png, but not in your description.
  19.     color <- sample(c("green","black","blue"),45,replace=TRUE)
  20.  
  21.     # and finally, which level does each segment need to belong to?
  22.     # you have 8 line levels in your example png. This works, might take
  23.     # some staring though:
  24.     level <- 0
  25.     for (i in 0:7){
  26.     level[position %in% ((i*600):(i*600+599))] <- 8-i
  27.     }
  28.  
  29.     # now stick into data.drame:
  30.     AminoData <-data.frame(position = position, xpos = xpos, posorneg = posorneg,
  31.          oxconc = oxconc, oxitype = oxitype, level = level, color = color)
  32.        
  33. # now we draw the base plot:
  34.     par(mar=c(3,3,3,3))
  35.     plot(NULL, type = "n", axes = FALSE, xlab = "", ylab = "",
  36.          ylim =  c(0,9), xlim = c(-10,609))
  37.     # horizontal segments:
  38.     segments(0,1:8,599,1:8,gray(.5))
  39.     # some ticks: (also not pretty)
  40.     segments(rep(c((0:5)*100,599),8), rep(1:8,each=7)-.05, rep(c((0:5)*100,599),8),
  41.        rep(1:8,each=7)+.05, col=gray(.5))
  42.     # label endpoints:
  43.     text(rep(10,8)+.2,1:8-.2,(7:0)*600,pos=2,cex=.8)
  44.     text(rep(589,8)+.2,1:8-.2,(7:0)*600+599,pos=4,cex=.8)
  45.     # now the amino line segments, remember segments() is vectorized
  46.     segments(AminoData$xpos, AminoData$level, AminoData$xpos,
  47.        AminoData$level + .5 * AminoData$posorneg, lty = AminoData$oxitype,
  48.        lwd = AminoData$oxconc, col = as.character(AminoData$color))
  49.     title("mostly you just need to reshape and preparenyour data to do this easily in base")