Two Lines of Labels for Two Grouping Factors of Boxplots

20 views
Skip to first unread message

Dario Strbenac

unread,
Nov 18, 2014, 9:00:09 PM11/18/14
to ggp...@googlegroups.com
It's possible to easily do with base graphics :

data <- data.frame(x = rep(LETTERS[1:2], each = 10), y = rep(LETTERS[3:4], 10), z = rnorm(20))
boxplot(z ~ x:y, data = data, axes = F, col = rep(c("red", "blue"), each = 2))
mtext(c("A", "B"), 1, 0, at = c(1.5, 3.5))
mtext(rep(c("C", "D"), 2), 1, 1, at = 1:4)
mtext(expression(bold("   Region")), 1, 0, at = 0.2)
mtext(expression(bold("Treatment")), 1, 1, at = 0.2)
axis(1, col = "black", labels = F)
axis(2)

How can I do the same with ggplot2 boxplots ? I don't want the factor y to be a boxplot colour, as previous solutions have done. In fact, I intend the boxes to be coloured by factor x.

Ista Zahn

unread,
Nov 18, 2014, 10:08:40 PM11/18/14
to Dario Strbenac, ggplot2
It won't be easy to recreate that in ggplot2. You can start with

names(data) <- c("Region", "Treatment", "z")
(p <- ggplot(data, aes(x = Treatment, y = z, fill = Treatment)) +
geom_boxplot() +
facet_wrap(~ Region) +
theme_classic() +
theme(strip.background = element_blank()))

and then do major surgery along the lines[1] of

library(gtable)
g <- ggplotGrob(p)
strips <- gtable_filter(g, "strip_t", trim=FALSE)
grid.newpage()
grid.draw(rbind(g, strips[3,], size="first"))


but it will be a fight.


Best,
Ista

[1] This code comes from
http://stackoverflow.com/questions/18065319/flip-facet-label-and-x-axis-with-ggplot2
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages