Hello everyone,
I am trying to build a user-defined function which basically returns a ggplot of multiple function curves in a single plot. The plot will be a complex version of the plot below, which basically plots 4 curves in one plot:
library(ggplot2)
p <- ggplot() + xlim(0,4) + xlab("x")
p <- p + geom_function(fun = function(x) 1*x)
p <- p + geom_function(fun = function(x) 2*x)
p <- p + geom_function(fun = function(x) 3*x)
p <- p + geom_function(fun = function(x) 4*x)
p
Unfortunately, I am not being able to plot it using a user defined function as below. It only returns me the last plot, out of the four plots:
plot_func <- function(i) {
p <- ggplot() + xlim(0,i) + xlab("x")
for (i in 1:i) {
p <- p + geom_function(fun = function(x) i*x)
}
print(p)
}
plot_func(4)
Can anyone please tell me how to plot this using a user-defined function?
Thank you very much!!