I am attempting to make a boxplot where different columns have different X axes. My solution was to create new variable that combine to different factors and let the faceting separate them. It does this, but it retains all of the factor levels for both columns even though there is no data for many of them.
I would like the 'cyl' column to just have the cyl levels and the 'trans' column to just have the trans levels instead of each having all the levels for both.
Some example data.
ids<-c("Ygrp","Xgrp","Xlab","Vals")
dtest<-rbind(setNames(cbind(rep("cty",234),rep("cyl",234), mpg[,c("cyl","cty")]),ids),
setNames(cbind(rep("cty",234),rep("trans",234), mpg[,c("trans","cty")]),ids),
setNames(cbind(rep("hwy",234),rep("cyl",234), mpg[,c("cyl","hwy")]),ids),
setNames(cbind(rep("hwy",234),rep("trans",234), mpg[,c("trans","hwy")]),ids))
p <- ggplot(dtest, aes(x = reorder(Xlab, Vals, FUN=median, na.rm=TRUE), Vals))
p + facet_grid(Ygrp~Xgrp)+
geom_boxplot()