2

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!

massisenergy
  • 1,764
  • 3
  • 14
  • 25
  • 2
    I don't exactly understand what you are trying to do here. Can you make this question reproducible and show expected output for the same? – Ronak Shah Jun 12 '20 at 01:02
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly are you passing to this function? – MrFlick Jun 12 '20 at 01:09
  • Wanted to automate some graph plotting using simple functions, mostly it was some mistake, but it's working now. I was getting the whole `bquote('Flow rate ('*mu~'litre)')` printed instead of *the function* of the `bquote`. Added the reproducible code now. Thanks for looking into this, anyway. – massisenergy Jun 12 '20 at 17:11

2 Answers2

2

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)

massisenergy
  • 1,764
  • 3
  • 14
  • 25
  • Happens to be the [Blog](https://aosmith.rbind.io/) of [aosmith](https://stackoverflow.com/users/2461552/aosmith), very useful & easy to understand resource... – massisenergy Jun 15 '20 at 20:34
2

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')
nniloc
  • 4,128
  • 2
  • 11
  • 22
  • Yeah, that makes it cleaner but as it says @https://ggplot2.tidyverse.org/reference/aes_.html#life-cycle, `aes_string` might be deprecated soon. – massisenergy Jun 12 '20 at 17:36
  • 1
    Good point. For reference I was trying to get [this](https://stackoverflow.com/a/49647973/12400385) answer with `!!` to work, but couldn't quite get it. Found my way [here](https://stackoverflow.com/a/22309328/12400385) for `aes_string`, but it is an answer from 2014. – nniloc Jun 12 '20 at 18:08