Plot a function with several arguments in R

前端 未结 4 2120
萌比男神i
萌比男神i 2020-12-19 22:47

Suppose I want to plot an R function:

weibull <- function(ALPHA, LAMBDA, T){
ALPHA*LAMBDA*(T^(ALPHA-1))
}

So the function takes the arg

4条回答
  •  有刺的猬
    2020-12-19 23:36

    I'd do it with plyr and ggplot2,

    enter image description here

    weibull <- function(alpha, lambda, time){
      data.frame(time = time, value = alpha*lambda*(time^(alpha-1)))
    }
    
    library(plyr)
    library(ggplot2)
    
    params <- expand.grid(lambda = c(1, 2, 4, 8, 16), alpha = c(0.5, 1))
    
    all <- mdply(params, weibull, time = seq(0, 2, length=100))
    
    ggplot(all, aes(time, value, colour=factor(lambda)))+
      facet_wrap(~alpha,scales="free", ncol=2) + geom_line()
    

提交回复
热议问题