R: ggplot2: facet_grid: how to include mathematical expressions in several (not all) labels?

I am stuck with something on ggplot2 . I read most related posts, tried things, but did not find a real solution.

I want to include math expressions in the label of my facet_grids with ggplot2 .

  • In the source file I can not write the name μg.L-1
  • In the headers and axis I can do this, for example: qplot(day, activity, data=a) +xlab(expression("100 µg "*.L^"-1"*"")) : this works well.
  • How to make for facet_labels? I can set levels and rename label factors, but the expression is not taken into account, for example:

    levels(a$group) <- c("control", expression("100 µg "*.L^"-1"*""))

    qplot(…, facets=~group)

Results:

Face label 1 is written on the graph: control

The inscription on the verge 2 is written on the chart: "100 mcg". L ^ "- 1" "" ...

and I don’t want that.

I do not want to use facet_grid(.~group, labeller=label_bquote(…)) because I do not want all my labels to match the same expression. I want to edit tags one by one manually . I tried with bquote(…) instead of expression(…) , but the same bad result will happen.

Does anyone have any clues?


Example: I define a dataframe:

 activity<- as.numeric(c("44","41","48","43","42","45","44","39", "47", "68", "88", "57")) group<-c("first","first","first","first","first","first", "second","second","second","second","second","second") day<- c("0", "0", "0", "20","20", "20","0", "0", "0", "20","20", "20" ) a<-data.frame(activity, group, day) 

I will speak:

 require (ggplot2) 

qplot(day, activity, facets=.~group, data=a, ylim=c(25,90))

enter image description here

I want to change the name of the facet labels and the y axis:

 levels(a$group)<- c("control", expression("100 µg "*.L^"-1"*"")) qplot(day, activity, facets=.~group, data=a, ylim=c(25,90), ylab=expression("fmol "*.µl^"-1"*"")) 

enter image description here

It works well with the y axis, however for the facet label it doesn't work ... Any hint?

+6
source share
2 answers

Proposed Solution:

Prequisite:

 activity <- as.numeric(c("44", "41", "48", "43", "42", "45", "44", "39", "47", "68", "88", "57")) group <- c("first", "first", "first", "first", "first", "first", "second", "second", "second", "second", "second", "second") day <- c("0", "0", "0", "20", "20", "20", "0", "0", "0", "20", "20", "20") a <- data.frame(activity, group, day) require(ggplot2) levels(a$group) <- c("control", expression("100 µg " * .L^"-1" * "")) 

Proposed Solution:

 p1 <- qplot(day, activity, data = a) p1 + facet_grid(. ~ group, labeller = label_parsed) 

result:

enter image description here

Description

We create a label structure as a line, where we create a formula, noting the use of ~ to replace spaces ... Then we say facet_grid() to parse the label string passed to it as a formula by setting labeller = label_parsed ...

Note. Display details are described in ?plotmath , but note that geom_text() and facet_grid() use strings, not expressions.

I hope this helps.

Reference:

See the Hagley Wickham page on the labels ...: https://github.com/hadley/ggplot2/wiki/labeller

+3
source

You can use label_parsed if all of your labels are valid expressions (you can put text inside quotes if necessary)

 library(ggplot2) levels(a$group)<- c("'control test'", "100~mu*g*'.L'^-1") ggplot(a) + facet_grid(.~group, labeller=label_parsed) 

enter image description here

+4
source

All Articles