Home > Back-end >  ggplot within custom function does not render the plot
ggplot within custom function does not render the plot

Time:01-17

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?

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • Related