I have the following dataset (it can be loaded into an object named df
using dget
).
structure(list(Group = c("ko", "ko", "ko", "ko", "ko", "ko",
"ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko",
"ko", "ko", "ko", "ko", "ko", "ko", "ko", "wt", "wt", "wt", "wt",
"wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt",
"wt", "wt", "wt", "wt", "wt", "ko", "ko", "ko", "ko", "ko", "ko",
"ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko",
"ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko", "ko",
"wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt",
"wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt", "wt"), Measurement = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("0.03",
"0.3", "3", "100"), class = "factor"), BP = c(9.548885417, 11.76753125,
10.81886833, 3.735009375, 15.14638354, 21.86461458, 72.18635417,
40.67140625, 39.62583333, 41.04598563, 52.39260417, 84.63145833,
128.4452083, 65.6888125, 74.09927083, 61.35260417, 104.3070833,
122.5025, 220.0510417, 113.1284375, 138.5880521, 102.530625,
191.6270521, 196.9140625, 26.59260417, 40.0754375, 29.32135417,
13.63532292, 28.6171875, 112.8878125, 122.7751042, 120.5545833,
58.8775, 126.1076042, 161.0260417, 152.7265625, 177.0934375,
84.11302083, 183.4596875, 251.1814583, 241.57, 266.7258333, 150.8433333,
296.5019792, 36.90447917, 10.32471823, 3.762708333, 14.453625,
8.382519792, 13.66830615, 13.07424167, 132.9072917, 54.18072917,
54.71140104, 62.12114583, 51.83520833, 52.69563542, 69.86561458,
203.15, 103.3058333, 110.7737552, 102.7642708, 91.0596875, 92.04659375,
104.1230417, 290.936875, 159.2723675, 128.1846875, 165.3551042,
157.8347708, 125.3124713, 181.0304244, 2.643285208, 2.702447917,
13.11195833, 5.448403958, 4.574270833, 19.26659375, 38.951875,
72.7571875, 54.02702083, 38.16339583, 43.743, 63.27578125, 110.4180208,
110.9884375, 59.8784375, 88.40447917, 107.5211458, 180.7617708,
158.3405208, 110.6219167), Age = c(60L, 60L, 60L, 60L, 60L, 60L,
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L,
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L,
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L)), .Names = c("Group",
"Measurement", "BP", "Age"), row.names = c(NA, -92L), class = "data.frame")
I have two columns that act as categories. One is df$Group
and the other one is df$Age
.
I wish to do two levels of grouping for my boxplots. One based on Group
and the other one based on Age
.
The best result I have achieved is this:
ggplot(aes(y = BP, x = Measurement, fill = Group), data = df) +
facet_wrap(~`Age`, ncol = 2) + geom_boxplot()
However, I do NOT want the Age = 30
and Age = 60
to plotted be on two separate frames/plots. I want the Age
grouping to be on the same plot just like the Group
grouping. Therefore for each Measurement
category on the x axis
, I will have 4 boxplots in the end instead of two and the legend will show two levels of grouping
, one for Group
(as I already have) and one for Age
.
How can I achieve this?