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?
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.