Plotting a function curve in R with 2 or more vari

2020-04-19 07:16发布

问题:

How can I draw a curve for a function like vpd = function(k,D){exp(-k*D)} in R?

I want a plot of D vs vpd(0:1) assuming k is constant.

The only question I could find was How to plot a function curve in R . I've tried both of:

plot.function(vpd, from=0, to=1, n=101)

curve(vpd, from=0, to=1, n=101, add=FALSE, type = "l")

but only get

Error in -k * D : 'D' is missing

UPDATE: solved it!

vpd <- function(D,k=0.05){exp(-k*D)} # D is the x axis 
plot(vpd, from=1, to=100, ylim=0:1)

回答1:

While Mamoun Benghezal's answer works for functions you define yourself, there may be cases where you want to plot a predefined function that expects more than 1 parameter. In this case, currying is a solution:

library(functional)

k <- 0.05

vpd <- function(k,D){exp(-k*D)}
vpd_given_k <- Curry(vpd, k = 0.05)

curve(vpd_given_k, ylim = c(0, 1),
      from = 1, to = 100, 
      xlab = "D", ylab = paste("vpd | k = ", k))