I want to add multiple curves to one ggplot
. With the normal plot
and curve(add=TRUE)
i can loop the curve function and can add multiple curves/functions.
Since i want to rewrite my code for ggplot
, i'm trying to figure out how achieve this with ggplot.
I'm using the stat_function
and a for
loop, but ggplot
is not adding the curve
, just saving the last one.
Minimum reproducible example with a single line:
library(ggplot2)
p1<-ggplot()
for (i in 1:10){
p1<- p1 + stat_function(aes(x=1:200),fun = function(x) {x+i*3}, col=i)
print(p1)
}
What i think the code should do:
I loop multiple stat_function
and adding it to my plot p1
What i want to do:
I want alle lines to be added in one plot(p1
). The code as it is, just shows the last line of the loop, as if it is overwriting the plot always, but as far as i understand ggplot
this should just add a line. Also an explanation why this does not work would be nice, maybe i just don't understand ggplot
here