【问题标题】:including conditional elements in plotmath expression for ggplot2 subtitle在 ggplot2 字幕的 plotmath 表达式中包含条件元素
【发布时间】:2019-05-04 06:53:03
【问题描述】:

我正在尝试编写一个自定义函数,我想在 ggplot2 绘图字幕中显示效果大小估计及其置信区间。我正在使用plotmath 正确显示希腊字母和其他数学符号。

这就是我想要的两种字幕的样子-

为了实现这一点,我编写了一个简单的函数——

# set up
set.seed(123)
library(tidyverse)
library(cowplot)

# creating a fictional dataframe with effect size estimate and its confidence
# intervals
effsize_df <- tibble::tribble(
  ~estimate, ~conf.low, ~conf.high,
  0.25, 0.10, 0.40
)

# function to prepare subtitle
subtitle_maker <- function(effsize_df, effsize.type) {
  if (effsize.type == "p_eta") {
    # preparing the subtitle
    subtitle <-
      # extracting the elements of the statistical object
      base::substitute(
        expr =
          paste(
            eta["p"]^2,
            " = ",
            effsize,
            ", 95% CI",
            " [",
            LL,
            ", ",
            UL,
            "]",
          ),
        env = base::list(
          effsize = effsize_df$estimate[1],
          LL = effsize_df$conf.low[1],
          UL = effsize_df$conf.high[1]
        )
      )
  } else if (effsize.type == "p_omega") {
    # preparing the subtitle
    subtitle <-
      # extracting the elements of the statistical object
      base::substitute(
        expr =
          paste(
            omega["p"]^2,
            " = ",
            effsize,
            ", 95% CI",
            " [",
            LL,
            ", ",
            UL,
            "]",
          ),
        env = base::list(
          effsize = effsize_df$estimate[1],
          LL = effsize_df$conf.low[1],
          UL = effsize_df$conf.high[1]
        )
      )
  }

  # return the subtitle
  return(subtitle)
}

请注意,条件语句的代码只有一行不同:eta["p"]^2(如果是"p_eta")或omega["p"]^2(如果是"p_omega"),其余代码相同。我想重构这段代码以避免这种重复。

我不能有条件地将eta["p"]^2omega["p"]^2 分配给函数体中的不同对象(比如说effsize.text &lt;- eta["p"]^2),因为R 会抱怨找不到对象etaomega在环境中。

我该怎么做?

---------- 后记---------- ----------------

以下是用于创建上面显示的组合图的代码-

# creating and joining two plots (plot is shown above)
cowplot::plot_grid(
  # plot 1
  ggplot(mtcars, aes(x = wt, y = mpg)) + geom_blank() +
    labs(
      subtitle = subtitle_maker(effsize_df, "p_omega"),
      title = "partial omega"
    ),
  # plot 2
  ggplot(mtcars, aes(x = wt, y = mpg)) + geom_blank() +
    labs(
      subtitle = subtitle_maker(effsize_df, "p_eta"),
      title = "partial eta"
    ),
  labels = c("(a)", "(b)"),
  nrow = 1
)

【问题讨论】:

    标签: r ggplot2 plotmath


    【解决方案1】:

    quote 和 bquote 的组合会有所帮助,

    subtitle_maker <- function(d, type){
    
      et <- if(type == 'a') quote(eta) else if(type == 'b') quote(omega)
    
      bquote(.(et)['p']^2==.(d$x)~", 95% CI ["*.(d$y)*","*.(d$z)*"]")
    
    }
    
    d <- list(x=1,y=2,z=3)
    grid::grid.newpage()
    grid::grid.text(subtitle_maker(d,"a"), y=0.3)
    grid::grid.text(subtitle_maker(d,"b"), y=0.7)
    

    注意:或替换而不是bquote,这只是个人喜好

    subtitle_maker <- function(effsize_df, effsize.type) {
    
      effsize.text <- if (effsize.type == "p_eta") quote(eta["p"]) else 
        if (effsize.type == "p_omega") quote(omega["p"])
    
          base::substitute(
            expr =
              paste(effsize.text^2,
                " = ",
                effsize,
                ", 95% CI",
                " [",
                LL,
                ", ",
                UL,
                "]",
              ),
            env = base::list(effsize.text = effsize.text,
              effsize = effsize_df$estimate[1],
              LL = effsize_df$conf.low[1],
              UL = effsize_df$conf.high[1]
            )
          )
    }
    

    【讨论】:

      【解决方案2】:

      这是一个 unicode 解决方案。我使用 dplyr 中的 case_when 让生活更轻松。

      # function to prepare subtitle
      subtitle_maker <- function(effsize_df, effsize.type) {
        # preparing the subtitle
        subtitle <-
          # extracting the elements of the statistical object
          base::substitute(
            expr =
              paste(
                effsize_sym["p"]^2,
                " = ",
                effsize,
                ", 95% CI",
                " [",
                LL,
                ", ",
                UL,
                "]",
              ),
            env = base::list(
              effsize = effsize_df$estimate[1],
              LL = effsize_df$conf.low[1],
              UL = effsize_df$conf.high[1],
              effsize_sym = case_when(
                effsize.type == "p_eta" ~ "\U1D702",
                effsize.type == "p_omega" ~ "\U1D714",
                TRUE ~ NA_character_
              )
            )
          )
      
        # return the subtitle
        return(subtitle)
      }
      

      【讨论】:

      • 谢谢。在我接受这个答案之前只是一个简单的问题:unicode 解决方案是否可以跨不同的操作系统工作?我总是使用Windows 机器,这很有效,但我希望这也适用于其他操作系统,因为我的合作者大多使用unix 机器。
      • @IndrajeetPatil 我想是的。我正在运行 Ubuntu,它运行良好。
      • 啊,我以为你在 Windows 机器上。不幸的是,这在 Windows 上不起作用。这就是您使用代码得到的结果:gist.github.com/IndrajeetPatil/…您是否有其他不涉及更改字符编码的解决方案?
      猜你喜欢
      • 1970-01-01
      • 2013-08-09
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 2023-03-18
      相关资源
      最近更新 更多