2

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)
bee guy
  • 429
  • 7
  • 20
  • 1
    It seems to be the first level value, not the expression causing the problem. I guess `label_parsed` does not allow empty spacing in the labels. Changing this prints the correct expression – timfaber Sep 27 '17 at 09:49

1 Answers1

1

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)")))
A. Stam
  • 2,148
  • 14
  • 29
  • Thanks for the prompt answer. I should have read the linked answer more carefully and I also forgot to delete one level, when creating a smaller data.frame to dput here. – bee guy Sep 27 '17 at 12:26