Plotting this took me a while to figure out, after going through a bunch of posts. In my data, I have a control and an experimental group. Multiple measurements are taken for these groups at different time points. I want to boxplot the control and experimental groups at these different points on the same figure, side by side, without extra x-axis labels.
My data looks something like the following:
PN1.Rx age synthesis heart.incorp.uptake ....
1 control PN3 DNA 0.26
2 control PN11 protein ...
3 6OHDA PN3 DNA ...
4 6OHDA PN11 protein ...
I first need to re-order the age-factors so when I plot them according to age, PN11 doesn’t come before PN3:
set1$age = factor(set1$age, levels=c("PN3", "PN5", "PN8", "PN11", "PN15", "PN19"), ordered=TRUE)
c.a = subset(set1, cohort=='a' & synthesis=='dna')
boxplot(heart.incorp.uptake ~ age, data = subset(c.a, PN1.Rx=="control"), xlab="Age",
ylab="Heart Uptake", xaxt='n', style="quantile", ylim=c(0,max(c.a$heart.incorp.uptake)),
col="blue", boxwex=0.3, at=1:6-0.2)
boxplot(heart.incorp.uptake ~ age, data = subset(c.a, PN1.Rx=="6OHDA"), xlab="Age",
ylab="Heart Uptake", xaxt='n', style="quantile", col="red", add=TRUE,
The first boxplot makes the boxes for the control groups, the second for experimental groups. I’m plotting heart.incorp.uptake as a function of age (hence the level re-ordering).
The xaxt=’n’ makes the axis label disappear.
The key options are boxwex, which gives the width of the boxplot (I think the default is 1). The “at” option shifts the center of each box with respect to the default one. I have 6 time points, with 2 boxes per time point. Thus the “1:6+/-0.2” shift.
axis(1, at=1:6, labels=levels(set1$age), tick=TRUE)
title("Cohort a: Heart Uptake and DNA Synthesis")
legend("topright", c("Control", "6OHDA"), fill=c("blue","red"))
Finally, I set the axis tickmarks to between the two boxes. The end result is below: