2

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)
2
  • If you really want "a plot of D vs vpd(0:1) assuming k is constant (=0.05)", then that's not a 2D function plot, it's one single contour line of constant k(=0.05) on a 2D plot (please fix your title). Also, do you want a function that automatically generates a sensible range of (k,D) to show the interesting parts of the plot, or do you want to manually supply the range of k? Commented Apr 13, 2019 at 1:08
  • Also if you want to plot D vs vpd=0:1, then really you're exploring the reverse-function D = -log(vpd) / k Commented Apr 13, 2019 at 1:52

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

8 Comments

That nearly does it. The y-axis is right but I want the x-axis to be the length of D i.e. if D is between 0 and 20 that should be the scale on the axis rather than 0 to 1.
I am not entirely sure if I understand your comment because D is just a value and not a range. But I edited the answer such that the maximum of the x-axis is always D.
From the question, I think OP is hoping to plot against D and hold k constant. Of course, since D and k are interchangeable in this equation that's really just a matter of labeling.
The question is a little bit mysterious - what does plot "D vs vpd(0:1)" mean? Only "plot vdp vs D" makes sense for me; and this is what you also suggest. However, I don't understand the OP's comment if this is true. @user3725599 can you clarify how the final plot should look like?
Fixed it. Sorry for being vague. vpd <- function(D,k=0.05){exp(-k*D)} # D is the x axis plot(vpd, from=1, to=100, ylim=0:1)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.