在函数中将变量传递给 ggplot 的正确方法,而不是在 R 中解释为字符串

Right approach for passing a variable into ggplot in a function, not to be interpreted as strings in R

我的代码:

function (var1, var2) {
    if (var1 = flowrate) {label <- bquote('Flow rate ('*mu~'litre)'))}
    else if (var1 = stress) {label <- bquote('Average velocity ('*m^2*s^-1*')')}
    ggplot{} + 
    ... +
    ylab(label)
}

我希望 ylab(label) 部分使用公式,但以这种方式将其绘制并打印为带引号的字符串。例如。当 var1 = flowrate 时,它打印 bquote('Flow rate ('*mu~'litre)') 作为 y 轴,而不是解释真正的含义。这是类似的东西,但不适用于我的情况:assigning a string to an object without double quotes

这一定是一种非常粗糙的方法,因为我不知道正确的方法,但如果有任何修复此问题或更好的方法来实现此目的的建议,我将不胜感激。

感谢您的宝贵时间!

对于一些主要由于不完整 if-else 语句而发生的奇怪错误,我们深表歉意,但它现在正在运行。不删除问题,因为有人可能会觉得这很有用。改编自:

library(tidyverse)

     sampleData <- data.frame(Sample = sample(10:20, 8), randomNum = rnorm(8)
                              , fruit = fruit[48:55], Blender = 
                             c('a','b','c','a','b','c','a','b'))

     sampleData
#>   Sample   randomNum     fruit Blender
#> 1     14  0.50009746 mandarine       a
#> 2     10  1.37473946     mango       b
#> 3     11  1.71557219  mulberry       c
#> 4     18  0.40837073 nectarine       a
#> 5     16  2.18151795       nut       b
#> 6     20 -1.59481280     olive       c
#> 7     12 -0.01976708    orange       a
#> 8     17 -1.34631557    pamelo       b

     boxViolinPlot.fun <- function(data, fill, x, y) {
        # except `data`, other input variables should be double quoted!
        library(ggplot2)
        if (y == "randomNum") 
            {label <- bquote('Flow rate ('*mu~'litre)')}
        if (y == "Sample") 
            {label <- bquote('Average velocity at outlet ('*m^2*s^-1*')')}
        else {ylab <- 'ylab'}
        ggplot(data, aes(x = .data[[x]], y = .data[[y]], fill = .data[[fill]]
                         )) + geom_boxplot(# varwidth = .1, 
                width = 0.5, alpha = 0.75) + ylab(label) #+ scale_y_log10()
    }

    boxViolinPlot.fun(data = sampleData, x = "Blender", y = "randomNum", 
                  fill = "fruit")

reprex package (v0.3.0)

于 2020-06-12 创建

这是使用您的 sampleDataaes_string

的另一个选项
sampleData <- data.frame(Sample = sample(10:20, 8), randomNum = rnorm(8)
                         , fruit = fruit[48:55], Blender = 
                           c('a','b','c','a','b','c','a','b'))


plot_it <- function(df, x, y, fill) {

  if (y == 'randomNum') {label <- bquote('Flow rate ('*mu~'litre)')}
  else if (y == 'Sample') {label <- bquote('Average velocity ('*m^2*s^-1*')')}
  else {label <- 'ylab'}

  ggplot(df, aes_string(x, y, fill = fill)) + 
    geom_boxplot() +
    ylab(label)
}

plot_it(sampleData, "Blender", "randomNum", 'fruit')

编辑:由于 aes_string 已被软弃用,这里还有另一个选项(似乎是最新的)使用 sym!! 将引用的字符串更改为变量,如图所示 here

plot_it_sym <- function(df, x, y, fill) {

  vars <- syms(c(x, y, fill))

  if (y == 'randomNum') {label <- bquote('Flow rate ('*mu~'litre)')}
  else if (y == 'Sample') {label <- bquote('Average velocity ('*m^2*s^-1*')')}
  else {label <- 'ylab'}

  ggplot(df, aes(x = !!vars[[1]], y = !!vars[[2]], fill = !!vars[[3]])) + 
    geom_boxplot() +
    ylab(label)
}

plot_it_sym(sampleData, 'Blender', 'randomNum', 'fruit')