How do I add the legends of the boxplot at the bottom so that they show up as a secondary axis text line above the "Follow-up 1", "Follow-up 2" etc. line?
Asked
Active
Viewed 231 times
1 Answers
1
Do you mean something like this using facetting?
# Sample data
set.seed(123);
df <- cbind.data.frame(
y = rnorm(20),
Group = sample(c("UT", "F", "T"), 20, replace = TRUE),
x = sample(c("Follow-up 1", "Follow-up 2"), 20, replace = TRUE))
# Plot
gg <- ggplot(df, aes(x = x, y = y, fill = Group));
gg <- gg + geom_boxplot();
gg <- gg + facet_wrap(~ x, ncol = 3, scales = "free_x");
gg <- gg + theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank());
gg <- gg + labs(x = "x axis label", y = "y axis label");
Update
With group labels on x-axis
gg <- ggplot(df, aes(x = Group, y = y, fill = Group));
gg <- gg + geom_boxplot();
gg <- gg + facet_wrap(~ x, ncol = 3, scales = "free_x");
gg <- gg + labs(x = "x axis label", y = "y axis label");

Maurits Evers
- 49,617
- 4
- 47
- 68
-
Thank you. I was looking for a solution without using the facet_wrap function and have the F, T and UT as x axis text. – Mac Oct 27 '17 at 00:43
-
I've updated my answer to include the group labels on the x-axis. Is this what you're after? – Maurits Evers Oct 27 '17 at 00:47