Box and Whiskers, Violin

Variation

Box and Whisker

Use

Arguments

points.x
- All X values of points that will be plotted
points.y
- All Y values of points that will be plotted
minimum.required
- Minimum number of points required to form a box plot
black.and.white
- Logical, Describes whether the line should be for grey scale or color output

Notes

Code

'plot.box.and.whiskers' <-
function(
  points.x = x,
  points.y = y,
  minimum.required = 6,
  black.and.white = black.and.white.in.panel
){
cat("\nPlotting Box and Whiskers\n")
  ### Box Plot Box Color (Default color is blue)
  box.rectangle <- trellis.par.get("box.rectangle")
  box.rectangle$col <- "black"
  trellis.par.set("box.rectangle", box.rectangle)

  ### Box Plot Outliers
  outlier.color <- "black"
  plot.symbol <- trellis.par.get("plot.symbol")
  plot.symbol$col <- outlier.color
  plot.symbol$cex <- 1
  trellis.par.set("plot.symbol", plot.symbol)

  ### Box Plot Whiskers
  whisker.color <- "black"
  box.umbrella <- trellis.par.get("box.umbrella")
  box.umbrella$col <- whisker.color
  trellis.par.set("box.umbrella", box.umbrella)


  if(black.and.white){
    boxfill.color <- "grey 90"
  }else{
    boxfill.color <- "light yellow"
  }
    
  table.points.x <- table(points.x)
  locations.x <- table.points.x[table.points.x >= minimum.required]
  x.names <- as.numeric(names(locations.x))

  if(length(x.names) > 0){
    x.box.points <- points.x[points.x %in% x.names]
    y.box.points <- points.y[points.x %in% x.names]
      ### Select only locations that contained the minimum requirement

#cat("Table of X.Points\n");print(table.points.x)
#cat("X Points that Passed");print(x.names)
#cat("x.box.points");print(x.box.points)
#cat("y.box.points");print(y.box.points)

    panel.bwplot(
      x = x.box.points,
      y = y.box.points,
      horizontal = FALSE,
      color = "black",  ### Color of "dot at median"
      pch="|",  ### Creates a horizontal line at the median, instead of a solid dot.  Omit if you like the 'dot' better.
      fill = boxfill.color
    )
  }else{
    cat("\nWarning!\n
          \tPlease lower the minimum required for the box and whisker plot to allow boxes to be plotted.\n
          \t(Only if desired)\n")
  }
}

Legend Info

key = list(
  title = "Legend",
  text = list(
    c("Box and Whisker Outliers"),
    col="black"
  ),
  points = list(
    col = c("black"),
    pch = c(1),
    cex = c(1.5)*0.75
  ),
  lines = list(
    col = c("black"),
    lwd = c(1)*0.75,
    lty = c("blank")
  ),
  type = "l",
  space = "right",
  cex.title = 1,
  cex = 0.75
)


Violin Plot

Use

Arguments

points.x
- All X values of points that will be plotted
points.y
- All Y values of points that will be plotted
cut.off
- Where should the violin stop at the ends
- If cut.off == 0, the violins stop at the most extreme points and do not continue
- If cut.off > 0, the violins continue past the most extreme values. The larger the number, the farther they extend
violin.color
- Default to "transparent". Typical color values may be used. "transparent" allows the inside of the plot to be 'see-through'.
violin.ghost
- Percentage of transparency. Range of values = [0,1] with 1 being solid and 0 being invisible.

Notes

Code

'plot.violin' <-
function(
  points.x = x,
  points.y = y,
  cut.off = 0,
  violin.color = "transparent",
  violin.ghost = .5
){
cat("\nPlotting Violin Plot\n")

  ### Violin Color and Alpha (Default color is blue; Default alpha is 1)
  viol <- trellis.par.get("plot.polygon")
  viol$col <- violin.color
  viol$alpha <- violin.ghost
  trellis.par.set("plot.polygon", viol)
    
  panel.violin(
    x = points.x,
    y = points.y,
    horizontal = FALSE,
    cut=cut.off
  )
}

Legend Info

NA