0

I am trying to use ggplot within a custom function like

Ploy <- function(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width, 
                   Petal.Width = Petal.Width){

  #Calculate some derived parameters
  deltak <- (Sepal.Length - Sepal.Width)/390
  ARk <- Petal.Width*2
  
  dat <- cbind.data.frame(deltak, ARk)
  
  #Fitting quadratic model
  mod <- lm(deltak ~ poly(ARk, 2, raw = TRUE))
  dat$pred = predict(mod, newdata = dat)
  
  #Plotting using ggplot2
  require(ggplot2)
  myplot <- function(mydf, xcol, ycol){
    ggplot2::ggplot(data = mydf, aes(x = {{xcol}}, y = {{ycol}})) + 
      ggplot2::geom_point(color='red', alpha=0.3, size=3)+
      ggplot2::stat_smooth(method='lm', formula = y~poly(x,2), se = F) +
      ggplot2::theme_bw() +
      ggplot2::xlab("X-axis") +
      ggplot2::ylab("Y-axis")+
      ggplot2::theme(panel.grid = element_blank())}
  myplot(dat, ARk, deltak)
  
  deltaK0 <- abs(mod$coefficients[[1]])
  
  return(c(`DeltaK0` = deltaK0))
}

When I am calling the function as

Ploy(Sepal.Length = iris$Sepal.Length, Sepal.Width = iris$Sepal.Width, 
     Petal.Width = iris$Petal.Width)

It does not return me the plot. How can I have the plot as the output of the function? How can I use data argument within the function?

1
  • you can specify input arguments to the function as strings V1 = "Sepal.width" and then use .data[[V1]] inside the function to refer to those columns of data. But it's hard to say without example data. Commented Jan 17, 2022 at 9:57

2 Answers 2

2

You have to explicitly return the plot object. Keeping with your syntax:

[...]
     my_plot <- myplot(dat, ARk, deltak)
      
     deltaK0 <- abs(mod$coefficients[[1]])
      
     return(
      list(
       'DeltaK0' = deltaK0,
       'Plot' = my_plot 
      )
     )
}

And then call the function and plot with

output <- Ploy(...)
output$Plot 
output$deltaK0
Sign up to request clarification or add additional context in comments.

Comments

1

This should do the trick for you (per @SEcker's answer)

Ploy <- function(Sepal.Length = Sepal.Length, Sepal.Width = Sepal.Width, 
                 Petal.Width = Petal.Width){
    
    #Calculate some derived parameters
    deltak <- (Sepal.Length - Sepal.Width)/390
    ARk <- Petal.Width*2
    
    dat <- cbind.data.frame(deltak, ARk)
    
    #Fitting quadratic model
    mod <- lm(deltak ~ poly(ARk, 2, raw = TRUE))
    dat$pred = predict(mod, newdata = dat)
    
    #Plotting using ggplot2
    require(ggplot2)
    myplot <- function(mydf, xcol, ycol){
        ggplot2::ggplot(data = mydf, aes(x = {{xcol}}, y = {{ycol}})) + 
            ggplot2::geom_point(color='red', alpha=0.3, size=3)+
            ggplot2::stat_smooth(method='lm', formula = y~poly(x,2), se = F) +
            ggplot2::theme_bw() +
            ggplot2::xlab("X-axis") +
            ggplot2::ylab("Y-axis")+
            ggplot2::theme(panel.grid = element_blank())}
   my_plot = myplot(dat, ARk, deltak)
    
    deltaK0 <- abs(mod$coefficients[[1]])
    
    my_list = list(
        'DeltaK0' = deltaK0,
        'Plot' = my_plot 
    )
    
   return(my_list)

}


output = Ploy(Sepal.Length = iris$Sepal.Length, Sepal.Width = iris$Sepal.Width, 
     Petal.Width = iris$Petal.Width)
output$Plot`

OLD CODE

my_plot = function(DF=df, X="", Y="", GROUP=""){

# Create ggplot2 scatterplot
p1 <- ggplot(DF,               
             aes(x = .data[[X]],
                 y = .data[[Y]],
                 col = Species)) +
  geom_point()

return(p1)

}

plot_stuff = my_plot(DF=iris, X= "Sepal.Length",Y = "Sepal.Width", GROUP="Species")
plot_stuff

2 Comments

Now, it is working. can I use data argument within the function?
You should be able to, provided it's modified correctly, yes.

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.