在ggplot2字幕的plotmath表达式中包括条件元素

时间:2018-12-03 00:22:59

标签: r ggplot2 plotmath

我试图编写一个自定义函数,以在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 <- eta["p"]^2),因为R会抱怨对象{{在环境中找不到1}}和eta

我该怎么做?

----------------------脚本---------------------- ----------------

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

omega

2 个答案:

答案 0 :(得分:2)

这是unicode解决方案。我使用case_when中的dplyr来简化生活。

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

enter image description here

答案 1 :(得分:1)

引号和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]
        )
      )
}