bee guy
bee guy

Reputation: 439

How to use labeller = label_parsed to include expressions in some ggplot facet labels?

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)

Upvotes: 2

Views: 2928

Answers (1)

A. Stam
A. Stam

Reputation: 2222

There are two problems with your code, as far as I can see. First, when you parse the labels you need to replace spaces with the ~ character (this was also noted in the SO question you link). This is what's causing the error you see; the parser can't deal with the whitespace between 'Capped' and 'brood' in your first factor level. Second, you are assigning three factor levels while only two appear in your data.

Changing the first two lines of your 'Code' block to the following produces a correct graph:

df$Trait = as.factor(as.character(df$Trait))
levels(df$Trait) <- c("Capped~brood~cells", expression(sqrt("Colony weight (g)")))

Upvotes: 1

Related Questions