【问题标题】:using `plotmath` to display combination of subscript and `[ ]`使用 `plotmath` 显示下标和 `[ ]` 的组合
【发布时间】:2019-12-30 04:46:48
【问题描述】:

我想创建一个图,我想在其中显示平均值和该平均值的置信区间。为此,我使用plotmath。这是我所做的工作-

library(ggplot2)

ggplot(mtcars, aes(as.factor(cyl), wt)) + geom_boxplot() +
  labs(
    title = "Mean weight:",
    subtitle = parse(text = paste(
      "list(~italic(mu)==", 3.22, ",", "CI[95~'%'] ", "(", 2.87, ",", 3.57, "))",
      sep = ""
    ))
  )

reprex package (v0.3.0) 于 2019 年 8 月 25 日创建

但这不是我真正想要的。我想要显示这些结果的格式如下-

所以有两件事我似乎无法弄清楚如何使用plotmath

  1. 95 % 应改为 95%

  2. 使用[ 而不是(

我该怎么做?

PS 重要的是,这里解释的原因很复杂,我在paste 函数内有list,因为我想将这些表达式保存为character-type数据框中的列。这就是为什么我没有接受下面提供的两种解决方案。

【问题讨论】:

    标签: r parsing ggplot2 expression plotmath


    【解决方案1】:

    一个选项是bquote

    library(ggplot2)
    ggplot(mtcars, aes(as.factor(cyl), wt)) + 
           geom_boxplot() +
           labs(title = "Mean weight:", 
            subtitle = bquote(italic(mu)~"= 3.22,"~CI[95*'%']~"["*"2.87, 3.57"*"]"))
    

    【讨论】:

    • 谢谢;这个问题有两个部分。您的回复仅解决了第一个问题。
    • @IndrajeetPatil 谢谢,我更新了bquote,因为它看起来更直接
    • 您介意提供一个不使用bquote并且更符合问题中现有代码的解决方案版本吗?
    • @IndrajeetPatil 我会这样做,但还有另一个答案这样做
    • 它没有。它既不使用list 也不使用paste,这对我来说很重要,因为我想将这些表达式保存为数据框中的一列。
    【解决方案2】:

    使用显示的公式:

    ggplot(mtcars, aes(as.factor(cyl), wt)) + geom_boxplot() +
      labs(
        title = "Mean weight:",
        subtitle = ~italic(mu) == 3.22*', '*"CI"[95*'%']*group('[',2.87*','*3.57,']')
      )
    

    【讨论】:

    • 谢谢。我更喜欢不更改问题中指定的代码的解决方案。我想使用list + paste,因为我将这些表达式保存为character 类型的列。
    • 只需将相同的表达式定义为字符串,在这种情况下,您可以以任何您喜欢的方式将其从元素中粘贴到一起。 subtitle <- "italic(mu) == 3.22*', '*'CI'[95*'%']*group('[',2.87*','*3.57,']')" 然后在ggplot中使用这个:subtitle = parse(text = subtitle)
    【解决方案3】:

    我假设您真正关心的是输出看起来是否正确,而不是使用 plotmath。您可以使用我目前正在开发的 ggtext 包,它使您可以在 ggplot2 中使用简单的 markdown/HTML。我通常发现以这种方式创建基本的数学表达式比使用 plotmath 更容易。而且你根本不需要使用 R 表达式,输入总是一个简单的字符串。

    # this requires the current development versions of ggplot2 and ggtext
    # remotes::install_github("tidyverse/ggplot2")
    # remotes::install_github("clauswilke/ggtext")
    
    library(ggplot2)
    library(ggtext)
    
    ggplot(mtcars, aes(as.factor(cyl), wt)) + 
      geom_boxplot() +
      labs(
        title = "Mean weight:",
        subtitle = "*&mu;* = 3.22, CI<sub>95%</sub>[2.87, 3.57]"
      ) +
      theme(plot.subtitle = element_markdown())
    

    reprex package (v0.3.0) 于 2019 年 12 月 2 日创建

    【讨论】:

    • 谢谢,克劳斯!我很高兴看到在ggplot 中显示表达式将来使用ggtext 会变得更容易。但是,目前,这个解决方案对我不起作用。我希望解决方案坚持格式(作为可以解析的character 类型列中的list),因为这是我需要在包函数中实现的格式:github.com/IndrajeetPatil/ggstatsplot/blob/…
    【解决方案4】:

    此解决方案保留列表和粘贴。

    library(ggplot2)
    
    ggplot(mtcars, aes(as.factor(cyl), wt)) + geom_boxplot() +
      labs(
        title = "Mean weight:",
        subtitle = parse(text = paste(
          "list(~italic(mu)==", 3.22, ",", "CI[95*'%'] ", "*'['*", 2.87, ",", 3.57, "*']')",
          sep = ""
        ))
      )
    
    

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2022-07-31
      • 2016-05-09
      • 2019-10-04
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2014-05-27
      相关资源
      最近更新 更多