I am trying to include expressions in a few, but not all ggplot facet labels. This question has been asked and resolved before, but I do not manage to make it work for my particular case.
I get the following error:
Error in parse(text = as.character(values)) : :1:8: unexpected symbol 1: Capped brood ^
Please let me know what my mistake is and how I can plot the square-root symbol in the facet label.
Data:
df <- structure(list(t0 = c(122.883911266139, 169.48220216013, 121.371211764444,
170.60152096912, 122.001873322486, 151.578850452806, 120.458523959951,
149.550874882101, 3411.77083333332, 8846.04166666666, 3329.0625,
8866.04166666666, 5931.79935833674, 4554.92435833674, 5618.75,
5387.63269167007), Treatment = structure(c(1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), Trait = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Capped brood cells",
"Colony strength", "sqrt(\"Colony weight (g)\")"), class = "factor"),
Timepoint = structure(c(1L, 2L, 1L, 2L, 3L, 4L, 3L, 4L, 1L,
2L, 1L, 2L, 3L, 4L, 3L, 4L), .Label = c("2013 Before", "2013 After",
"2014 Before", "2014 After"), class = "factor")), .Names = c("t0",
"Treatment", "Trait", "Timepoint"), row.names = c(NA, 16L), class = "data.frame")
Code:
df$Trait = as.factor(df$Trait)
levels(df$Trait) <- c("Capped brood cells", "Colony strength", expression(sqrt("Colony weight (g)")))
dodge <- position_dodge(width=0.9)
ggplot(df, aes(x = Timepoint, y = t0, fill = Treatment))+
geom_bar(position = dodge, stat="identity", color = "black")+
facet_grid(Trait~., scales = "free_y", labeller = label_parsed)