I got this function.
change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
I would like to plot, in one single plot, all results of this function for a range of arguments d and p. In base r it would be this.
parameters <- c(100:400)
colorshelf <-rainbow(length(parameters)) #red is low
for(i in seq_along(parameters)) {
print(i)
curve(change(score=1, d=x, k=100, p=parameters[i]), from=0, to=500, add=T, col=colorshelf[i])
}
But I thought this must be possible in ggplot2, but can not wrap my head around this. I am currently stuck with this. Any help is appreciated.
ggp <- ggplot(data.frame(Ds=c(0:1000), Ps=c(0:1000)), aes(x=Ds, col=Ps)) +
stat_function(fun=change, args=list(score=1, d=Ds, k=100, p=Ps))
ggp
I would do this outside of ggplot2. I think it might be too much to expect ggplot to vectorise over two different parameters ...
This is with tidyverse, but could easily be done with apply as well.
library(dplyr)
change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
dd <- expand.grid(d=0:1000,p=0:100)
dd %>% rowwise %>%
mutate(c=change(score=1,d=d,k=100,p=p)) ->
dd2
library(ggplot2)
ggp <- ggplot(dd2,aes(d,c,col=p,group=p))+
geom_path()
I only did p from 0 to 100 (rather than 0 to 1000) because 1 million points is a fairly large data set for ggplot. (Do you really need to see 1000 separate values? Maybe seq(0,1000,length=100) ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With