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

Viewed 68

My code:

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

I want the ylab(label) part to use the formula, but in this way it plots and prints it as a quoted string. E.g. when var1 = flowrate, it prints bquote('Flow rate ('*mu~'litre)') as the y axis, not interpreting the real meaning. Here is something similar, but cannot apply in my case: assigning a string to an object without double quotes

This must be a very crude way since I don't know the proper approach, but I would appreciate any suggestion for fixing this or better ways to achieve this.

Thanks for your time!

2 Answers

Sorry for some weird error that was mostly happening due to incomplete if-else statements, but it's working now. Not deleting the question since someone might find this useful. Adapted from:

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")

Created on 2020-06-12 by the reprex package (v0.3.0)

Here is another option using your sampleData and aes_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')

EDIT: As aes_string is soft deprecated, here is yet another option (which seems current) using sym and !! to change the quoted string into a variable as shown 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')
Related