0

I have a simple code that creates a combined barplot and line plot with two axes. I wanted to make this code dynamic so that I can pass the columns to plot as parameters of a function. Here is the code outside the function,it works fine:

data <- data.frame(group = LETTERS[1:5],    # Create example data
                   sample = c(1000, 800, 1200, 900, 800),
                   responses1 = c(0.6, 0.9, 0.9, 0.5, 0.8),
                   responses2 = c(0.2, 0.4, 0.7, 0.1, 0.3)
                   )

ggp1 <- ggplot(data) +                     
  geom_bar(aes(group, sample), stat = "identity") + 
  geom_line(aes(group, responses * max(sample), group = 1),
            col = "#1b98e0", lwd = 3) + 
  scale_y_continuous(sec.axis = sec_axis(~ . / max(data$sample)))

ggp1 

Now when trying to put it in a function it becomes challenging a little bit for me:

plot_func <- function(var1, var2) {
  ggp1 <- ggplot(data) +                     
    geom_bar(aes(group, var1), stat = "identity") + 
    geom_line(aes(group, var2 * max(var1), group = 1),
              col = "#1b98e0", lwd = 3) + 
    scale_y_continuous(sec.axis = sec_axis(~ . / max(data[[var1]])))
  
  ggp1
}
9
  • 1
    If you want to pass them in unquoted you need to quote and unquote using either the older notation !!sym(x) or the newer {{x}}. Commented Feb 22, 2022 at 19:38
  • I tried using {{var1}} and {{var2}} but it doesn't work if that is what you mean. Commented Feb 22, 2022 at 19:43
  • 1
    Can you show the way you define when you call it in plot_func()? It should be plot_func(responses1) Commented Feb 22, 2022 at 19:45
  • Your first block of code plotting outside a function also doesn't work because responses is not defined anywhere. Commented Feb 22, 2022 at 19:50
  • 2
    Ok - will check. But just FYI it's a bad practice to write a function that relies on a specific dataset being defined in your global environment. Much better practice to pass the data to your function so it's self contained. Commented Feb 22, 2022 at 19:54

1 Answer 1

1

If you are passing var1 and var1 unquoted, you could try something like this:

plot_func <- function(df, var1, var2) {
  ggp1 <- ggplot(df) +                     
    geom_bar(aes(group, {{var1}}), stat = "identity") +
    geom_line(aes(group, {{var2}} * max({{var1}}), group = 1), col = "#1b98e0", lwd = 3) + 
    scale_y_continuous(sec.axis = sec_axis(~ . / max(df %>% pull({{var1}}))))
  ggp1
}

Then try:

plot_func(data,sample, responses1)

On the other hand, if var1 and var2 are like this "sample" and "responses1", respectively, you can define the function this way:

plot_func2 <- function(df, var1, var2) {

  var1_max = max(data[[var1]])
  data$line_y = data[[var2]]*var1_max
  
  ggp1 <- ggplot(df) +                     
    geom_bar(aes_string("group", var1), stat = "identity") +
    geom_line(aes_string("group", "line_y", "group" = 1), col = "#1b98e0", lwd = 3) +
    scale_y_continuous(sec.axis = sec_axis(~ . / var1_max))
  ggp1
}

And then use it like this:

var1="sample"
var2="responses1"
plot_func2(data,var1,var2)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the working example. I think I wasn't very clear about my problem. I don't know the parameters sample and responses1 to pass them this way: plot_func(data,sample, responses1). I store the chosen two columns selected by the user in two variables var1 and var2 and I wanted to be able to pass them directly like plot_func(data, var1, var2).
got it. sorry I did not understand
From your app, are the variables characters? That changes how you want to pull them into your function.
The user has the list of variables in two dropdown menus, so he selects the var1 and var2 which is are strings (names of columns). After that, I just should plug those two selected values into the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.