R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式? [英] R : ggplot2 : facet_grid : how include math expressions in few (not all) labels?

查看:247
本文介绍了R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ggplot2 中遇到了一些问题。我读了大部分相关文章,尝试了一些东西,但没有找到任何真正的解决方案。



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




  • ,我不能写出名称μg.L-1

  • 在title和axis中,我可以这样做,例如:
    qplot(day,activity ,数据= a)+ xlab(表达式(100μg* .L ^ - 1*)):这是行之有效的

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

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






    qplot(...,facets =〜group)
  • p>结果:


    标签facet 1写在图表上:control

    <图片上写有方面2的标签:100μg .L ^ - 1...



    $

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



    有人对此有任何线索吗?




    一个例子:我定义一个数据框:

      activity< - 如数字(44,41,48,43,42,45,44,39,47,68,88 ,57))
    group <-c(first,first,first,first,first,first,
    second,second ,秒,秒,秒,秒)
    日<-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))





    我想更改facet标签的名称和y轴:
    $ b $ pre $ 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*))



    解释



    我们创建标签结构作为字符串,我们创建一个公式,注意使用来替换空格...然后我们告诉 facet_grid() labeller = label_parsed ...



    来解析传递给它的标签字符串。注意:显示细节在?plotmath 中进行了描述,但请注意 geom_text() facet_grid()使用字符串,而不是表达式。



    我希望以上内容能够帮助您。



    参考:



    请参阅Hagley Wickham关于贴标签的页面: https://github.com/hadl ey / ggplot2 / wiki / labeller


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

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

    • In the raw file, I cannot write the name µ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.
    • 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 :

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

      qplot(…, facets=~group)

    Results :

    Label of facet 1 is written on the graph : control

    Label of facet 2 is written on the graph : "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

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

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

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

    解决方案

    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

    这篇关于R:ggplot2:facet_grid:如何在少数(不是全部)标签中包含数学表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆