Disclaimer: I'm a total newbie regarding R, especially data visualization; so I apologize if this is trivial. I just have no idea how to approach this.
I have four variables, say, X, Y, Z, W, each with a "high" and "low" value (e.g. big X and little x). Hence, there are 16 combinations, e.g. XYzw, XyZw, xyZw, etc. For each combination, I obtained a boxplot. (Each combination is an experiment that was executed multiples times, each spitting a numeric value.) I want to arrange these boxplots in one "meta" plot by arranging the combinations of two variables on the x-axis and the other two on the y-axis. For example, the x-axis could consist of the values XY, Xy, xY, xy, and the y-axis of the values ZW, Zw, zW, zw. Then, each point in the graph is a boxplot corresponding to a combination.
Edit 1:
My data has the following shape:
combs <- data.frame(xyzw=sample(1:100,10),xyzW=sample(1:100,10),xyZW=sample(1:100,10),xyZw=sample(1:100,10),
xYzW=sample(1:100,10),xYzw=sample(1:100,10),xYZW=sample(1:100,10),xYZw=sample(1:100,10),
XyzW=sample(1:100,10),Xyzw=sample(1:100,10),XyZW=sample(1:100,10),XyZw=sample(1:100,10),
XYzW=sample(1:100,10),XYzw=sample(1:100,10),XYZW=sample(1:100,10),XYZw=sample(1:100,10))
I could plot 16 boxplots by looping through each column, e.g. for (comb in combs) { boxplot(comb) }
, but how can I plot a graph where the x-axis has values XY, Xy, xY, xy, the y-axis has values ZW, Zw, zW, zw, and each data point is a boxplot?
CodePudding user response:
Thank you for updating your question! There are a few ways to handle this task. One potential option is to 'pivot' your data to 'long format' (https://tidyr.tidyverse.org/reference/pivot_longer.html), then separate the name column (i.e. xyzW) into two 'new' columns (i.e. "xy", "zw") using separate()
from the tidyr package, then use these 'new' columns to make the grid:
library(tidyverse)
combs <- data.frame(xyzw=sample(1:100,10),xyzW=sample(1:100,10),xyZW=sample(1:100,10),xyZw=sample(1:100,10),
xYzW=sample(1:100,10),xYzw=sample(1:100,10),xYZW=sample(1:100,10),xYZw=sample(1:100,10),
XyzW=sample(1:100,10),Xyzw=sample(1:100,10),XyZW=sample(1:100,10),XyZw=sample(1:100,10),
XYzW=sample(1:100,10),XYzw=sample(1:100,10),XYZW=sample(1:100,10),XYZw=sample(1:100,10))
combs %>%
pivot_longer(everything()) %>%
separate(name, into = c("xy", "zw"), sep = c(2,4)) %>%
ggplot(aes(x = "", y = value))
geom_boxplot()
facet_grid(rows = vars(xy), cols = vars(zw), scales = "free")
theme(axis.ticks.x = element_blank(),
axis.title.x = element_blank())
Created on 2022-10-04 by the reprex package (v2.0.1)
Does this approach work with your actual data?