R : ggplot2 : facet_grid : how include math expressions in few (not all) labels?

萝らか妹 提交于 2019-11-30 12:38:38

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:

Explanation

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

Note: The details of the display are described in ?plotmath, but note that geom_text() and facet_grid() use strings, not expressions.

I hope the above helps.

Reference:

See Hagley Wickham's page on labellers...: https://github.com/hadley/ggplot2/wiki/labeller

You can use label_parsed, provided all 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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!