ggplot Exponents and line feeds in facet labels

I am trying to get facet labels with a new line and exponents and I can get one or the other but not both. Code for the two possibilities is below

The first attempt correctly gives the exponent in the top facet label but I would like the volumetric water content to be on two lines.
The second attempt correctly puts the volumetric water content on two lines but the exponent in the top facet label is not correct, and the % in the second one is not correct.

n2 <- NULL
n2$Animal <- c(rep("cattle",36),rep("sheep",36))
n2$LowHill <- c(rep("low",6),rep("med",6),rep("steep",6),rep("low",6),rep("med",6),rep("steep",6),
                      rep("low",6),rep("med",6),rep("steep",6),rep("low",6),rep("med",6),rep("steep",6))
n2$Variable <- c(rep("BD",12),rep("pH",12),rep("OP",12),rep("OC",12),rep("TN",12),rep("SW",12))

n2$Vars <- factor(n2$Variable, labels= c( 'bulk~density~(Mg/m^3)',"soil~organic~C~('%')", "Olsen~P~( \u03BCg/ml)","soil~pH", 'volumetric~water~content~\n~(v/v~average~of~first~30~days)',"soil~total~N"))  

n2$var <- rnorm(72,1,0.1)
n2 <- as.data.frame(n2)

pall <- ggplot(n2, aes(x=LowHill, y=var)) + geom_jitter(width=0.2,height=0) pall <- pall + geom_boxplot() pall <- pall + theme_bw() + ylab(" ") pall <- pall + facet_grid(Vars~Animal, scales="free_y",labeller=label_parsed ) pall <- pall + theme(strip.text.y = element_text(angle=0)) pall


#####################################
n2 <- NULL
n2$Animal <- c(rep("cattle",36),rep("sheep",36))
n2$LowHill <- c(rep("low",6),rep("med",6),rep("steep",6),rep("low",6),rep("med",6),rep("steep",6),
                      rep("low",6),rep("med",6),rep("steep",6),rep("low",6),rep("med",6),rep("steep",6))
n2$Variable <- c(rep("BD",12),rep("pH",12),rep("OP",12),rep("OC",12),rep("TN",12),rep("SW",12))

n2$Vars <- factor(n2$Variable, labels= c( 'bulk density (Mg/m^3)',"soil organic C ('%')", "Olsen P ( \u03BCg/ml)","soil pH", 'volumetric water content\n (v/v average of first 30 days)',"soil total N"))  

n2$var <- rnorm(72,1,0.1)
n2 <- as.data.frame(n2)

pall <- ggplot(n2, aes(x=LowHill, y=var)) + geom_jitter(width=0.2,height=0) pall <- pall + geom_boxplot() pall <- pall + theme_bw() + ylab(" ") pall <- pall + facet_grid(Vars~Animal, scales="free_y",labeller=labeller(type=label_parsed )) pall <- pall + theme(strip.text.y = element_text(angle=0)) pall

Welcome Alasdair!

As far as I know, it's not possible to have a single parsed expression with a line break (the \n is ignored). Instead, you can use the atop function, which puts one expression below another. Note the use of atop in the code below. The ~ is to delay evaluation of the expression until it's parsed to generate the facet labels:

n2$Vars <- factor(n2$Variable, 
                  labels= c( 'bulk~density~(Mg/m^3)',
                             "soil~organic~C~('%')", 
                             "Olsen~P~( \u03BCg/ml)","soil~pH", 
                              ~atop('volumetric water content', '(v/v average of first 30 days)'),
                             "soil~total~N"))

Also, for future reference, you can shorten your data frame creation code. For example:

Animal <- rep(c("cattle","sheep"), each=36)
LowHill = rep(rep(c("low","med","steep"), each=6), 4)
n2 = data.frame(Animal, LowHill)

The rep function can also do things like this:

rep(c("a","B","c"), c(2,10,3))

[1] "a" "a" "B" "B" "B" "B" "B" "B" "B" "B" "B" "B" "c" "c" "c"

3 Likes