Quantiles

Example - Points, Lines, and Quantiles

Code Used

#pdf(file="F:/Website/quantileplot/points-lines-quantile-large.pdf", width = 15, height = 7.5)
### Import Data  
  data <- infuse
  split.column <- "DOSE" 
    
  data <- data[data$DOSE == 120000,]

  data.select <- data[ data$MDV == 0 & data$AMT == 0, ] 
    ### Rows where MDV == 0 and AMT == 0  (Good Rows for plotting x,y)
    
  data.to.plot <- list()
  
  data.to.plot$x <- data.select[,"TIME"]  #x
  data.to.plot$y <- data.select[,"DV"]  #y
  data.to.plot$ids <- data.select[,"ID"]  #ids
  
  data.to.plot$criteria <- data.select[,split.column]

  x.min.lim <- 216
  x.max.lim <- 288
  y.min.lim <- 0
  y.max.lim <- max(data.to.plot[["y"]])
  data.to.plot$x.lim <- c(x.min.lim,x.max.lim)
  data.to.plot$y.lim <- c(y.min.lim,y.max.lim)

  quan <- infuse[ infuse$MDV == 0 & infuse$AMT == 0, ]

  quan.select <- quan[quan$DOSE == 120000, ]

  quantiles.to.plot <- list()

  quantiles.to.plot$x <- quan.select[,"TIME"]  #x
  quantiles.to.plot$y <- quan.select[,"DV"]  #y
  quantiles.to.plot$alpha <- 0.05  #alpha (percent from 0 and 1, i.e. alpha==0.05 for 5% and 95% intervals)

  bw <- FALSE

  library(lattice)
### End Import


### Panel Function

  function.of.panel <- function(
    x,
    y,
    groups = groups,
    data.to.plot.in.panel = data.to.plot,
    quantiles.to.plot.in.panel = quantiles.to.plot,
    black.and.white.in.panel = bw,
    subscripts
  ){
  cat("\n\n\nPacket", packet.number(),"\n")
  
  ### Grid
    plot.grid(
      x.divisions = 10,
      y.divisions = 10,
      grid.color = "grey95",
      x.limits = data.to.plot.in.panel[["x.lim"]],
      y.limits = data.to.plot.in.panel[["y.lim"]]
    )

  ### Connect Points by ID
    lines.by.id(
      points.x = x,
      points.y = y,
      ids = groups,
      rows.being.plotted = subscripts,
      black.and.white = black.and.white.in.panel
    )

  ### Quantiles
    plot.quantiles(
      quantile.data = quantiles.to.plot.in.panel,
      quantile.type = 7
    )

  ### Plot Points
    plot.points(
      points.x = x,
      points.y = y,
      points.symbol = "x",
      points.size = 1,
      points.color = "black"
    )

  }
      
### End Panel



### Make Quantile Plot
  lat.quantile <- xyplot(
    y ~ x | as.factor(criteria), 
      # Remove '| as.factor(criteria)' if you do not want to split the data
    data = data.to.plot,
    type = NULL,
      # Taken Care of in Panel
    aspect=.5,
      # X is twice as long as Y
    groups = ids,
    xlab = "Time [units]",
    ylab = "Response [units]",
    xlim = c(x.min.lim - 0.03*(x.max.lim-x.min.lim)*0.5,x.max.lim + 0.03*(x.max.lim-x.min.lim)*0.5), 
      #0.5 is to compensate for the aspect ratio (aspect)
    ylim = c(y.min.lim - 0.03*(y.max.lim-y.min.lim), y.max.lim + 0.03*(y.max.lim-y.min.lim)),
    main = "Infuse Data Set\nTime > 216 hr",
    panel = function.of.panel,
	
    key = list(
      title = "Legend",
      text = list(
        c("Lines by ID","Quantiles","Points"),
        col="black"
      ),
      points = list(
        col = c(rainbow(1,start=.56,end=.57),"grey35","black"),
        pch = c(NA,NA,"x"),
        cex = c(1)*0.75
      ),
      lines = list(
        col = c(rainbow(1,start=.56,end=.57),"grey35","black"),
        lwd = c(1,2,1)*0.75,
        lty = c("solid","dashed","blank")
      ),
      type = "l",
      space = "right",
      cex.title = 1,
      cex = 0.75
    )

  )

### Change Color of Trellis Device and Plot Data
  trellis.device(color = !bw,new = FALSE,retain = FALSE)

### Plot Data
  print(lat.quantile)

#dev.off()