代码之家  ›  专栏  ›  技术社区  ›  Indrajeet Patil

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

  •  1
  • Indrajeet Patil  · 技术社区  · 6 年前

    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" )剩下的代码是 完全相同的

    我不能有条件地分配 预计到达时间[“p”]^2 ω[“p”]^2 effsize.text <- eta["p"]^2 )因为 R eta omega 在环境中找不到。

    我该怎么做?

    下面是用于创建上述组合图的代码-

    # 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
    )
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Dan    6 年前

    这里有一个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

        2
  •  1
  •   user10737199    6 年前

    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)
    

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