R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

时间:2022-06-08 14:58:33

I get stuck with something on ggplot2. I read most of the related posts, tried things but did not find any real solution.

我在ggplot2上遇到了困难。我阅读了大部分相关帖子,尝试了一些但没有找到任何真正的解决方案。

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

我想用ggplot2在我的facet_grids的标签中包含数学表达式。

  • In the raw file, I cannot write the name µg.L-1
  • 在原始文件中,我不能写名称μg.L-1

  • In the titles and axis I can do it, for example : qplot(day, activity, data=a) +xlab(expression("100 µg "*.L^"-1"*"")) : this is working well.
  • 在标题和轴中我可以做到,例如:qplot(日,活动,数据= a)+ xlab(表达式(“100μg”* .L ^“ - 1”*“”)):这个效果很好。

  • How do I do for the facet_labels ? I can set the levels and rename the labels factors but the expression is not taken into account, for example :

    我该如何处理facet_labels?我可以设置级别并重命名标签因子,但不考虑表达式,例如:

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

    水平($ group)< - c(“control”,表达式(“100μg”* .L ^“ - 1”*“”))

    qplot(…, facets=~group)

Results :

Label of facet 1 is written on the graph : control

方面1的标签写在图表上:控制

Label of facet 2 is written on the graph : "100 µg ".L^"-1""" …

方面2的标签写在图表上:“100μg”.L ^“ - 1”“”......

and I don’t want that.

我不希望这样。

I don’t want to use facet_grid(.~group, labeller=label_bquote(…)) because I don’t want all my labels to follow the same expression. I want to edit the labels one by one manually… I tried with bquote(…) instead of expression(…) but the same bad result happens

我不想使用facet_grid(.~group,labeller = label_bquote(...)),因为我不希望我的所有标签都遵循相同的表达式。我想手动逐个编辑标签...我尝试使用bquote(...)而不是表达式(...)但是同样的坏结果发生了

Does someone have any clue with this?

有人对此有任何线索吗?


An 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 plot :

我的情节:

require (ggplot2) 

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

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

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

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

我想更改构面标签和y轴的名称:

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"*""))

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

It works well with the y-axis, however for the facet label, it does not work... Any clue ?

它适用于y轴,但对于刻面标签,它不起作用......任何线索?

2 个解决方案

#1


4  

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:

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

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...

我们将标签结构创建为一个字符串,我们在其中创建一个公式,注意使用〜来替换空格...然后告诉facet_grid()通过设置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.

注意:显示的详细信息在?plotmath中描述,但请注意,geom_text()和facet_grid()使用字符串,而不是表达式。

I hope the above helps.

我希望以上有所帮助。

Reference:

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

查看Hagley Wickham关于贴标签的页面......:https://github.com/hadley/ggplot2/wiki/labeller

#2


6  

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

您可以使用label_parsed,前提是您的所有标签都是有效的表达式(如果需要,您可以将文本放在引号内)

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

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

#1


4  

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:

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?

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...

我们将标签结构创建为一个字符串,我们在其中创建一个公式,注意使用〜来替换空格...然后告诉facet_grid()通过设置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.

注意:显示的详细信息在?plotmath中描述,但请注意,geom_text()和facet_grid()使用字符串,而不是表达式。

I hope the above helps.

我希望以上有所帮助。

Reference:

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

查看Hagley Wickham关于贴标签的页面......:https://github.com/hadley/ggplot2/wiki/labeller

#2


6  

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

您可以使用label_parsed,前提是您的所有标签都是有效的表达式(如果需要,您可以将文本放在引号内)

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

R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?