How to make nested x-labels look like a JMP volatility plot but using ggplot2
I like the JMP volatility graph. ( link ) This is a powerful tool.
In the example, the graph has two labels on the x-axis, one for the partial number and one for the operator.
There are more than two levels of variables displayed in the JMP variability plot. The following splits are for butter quantity, batch size, and popcorn. It may take some work to find the right sequence to show strong separation, but it is a great tool for communicating information.
How do I do this, multi-level x-labels, with R using the ggplot2 library?
The best I can find is ( link , link ), which separates based on the number of cylinders, but does not make X-axis labels.
My example code:
#reproducible
set.seed(2372064)
#data (I'm used to reading my own, not using built-in)
data(mtcars)
attach(mtcars)
#impose factors as factors
fact_idx <- c(2,8:11)
for(i in fact_idx){
mtcars[,i] <- as.factor(mtcars[,i])
}
#boxplot
p <- ggplot(mtcars, aes(gear, mpg, fill=cyl)) +
geom_boxplot(notch = TRUE)
p
The plot that this gives:
How do I get both gears and cylinders to display on the X axis?
source to share
You can use the VCA R package that comes with the varPlot function that implements JMP-like variance plots. The help system provides several examples. Your example would look like:
library(VCA)
dat <- mtcars[order(mtcars$cyl, mtcars$gear),]
# default
varPlot(mpg~cyl/gear, dat)
# nicely formatted
varPlot(mpg~cyl/gear, dat,
BG=list(var="gear", col=paste0("gray", c(90,80,70)),
col.table=T),
VLine=list(var="cyl"), Mean=NULL,
MeanLine=list(var=c("cyl", "gear"), col=c("blue", "orange"),
lwd=c(2,2)),
Points=list(pch=16, cex=1))
source to share