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)
smci
  • 32,567
  • 20
  • 113
  • 146
Josh J
  • 395
  • 1
  • 3
  • 13
  • 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? – smci Apr 13 '19 at 01:08
  • Also if you want to plot D vs vpd=0:1, then really you're exploring the reverse-function D = -log(vpd) / k – smci Apr 13 '19 at 01:52

1 Answers1

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))
CL.
  • 14,577
  • 5
  • 46
  • 73
  • 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. – Josh J Jul 20 '15 at 13:59
  • 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`. – CL. Jul 20 '15 at 15:53
  • 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. – Gregor Thomas Jul 20 '15 at 15:56
  • 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? – CL. Jul 20 '15 at 16:11
  • 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) ` – Josh J Jul 21 '15 at 14:19
  • The I suppose the (latest) update of the answer is what you need. – CL. Jul 21 '15 at 17:30
  • Thanks. What is the difference? I've managed to use the "my" same method for functions with multiple functions. – Josh J Jul 28 '15 at 14:25
  • Not sure if I understand your question. `Curry` allows you to "permanently" set an argument to a value. This won't be necessary if the argument has a (correct) default value, but you need it when there's no default or when you want to speficy another value than the default one. – CL. Jul 29 '15 at 07:11