0

So, I've spent the last four hours trying to find an efficient way of plotting the curve(s) of a function with two variables - to no avail. The only answer that I could actually put to practice wasn't producing a multiple-line graph as I expected.

I created a function with two variables, x and y, and it returns a continuous numeric value. I wanted to plot in a single screen the result of this function with certain values of x and all possible values of y within a given range (y is also a continuous variable).

Something like that:

enter image description here

These two questions did help a little, but I still can't get there:

Plotting a function curve in R with 2 or more variables

How to plot function of multiple variables in R by initializing all variables but one

I also used the mosaic package and plotFun function, but the results were rather unappealing and not very readable: https://www.youtube.com/watch?v=Y-s7EEsOg1E.

Maybe the problem is my lack of proficiency with R - though I've been using it for months so I'm not such a noob. Please enlighten me.

neilfws
  • 32,751
  • 5
  • 50
  • 63
Igor Calado
  • 39
  • 1
  • 1
  • 3
  • 4
    Please include the code you have tried; this will help others to help you. – Tim Biegeleisen Dec 19 '17 at 02:00
  • As-is, can't tell at all what your problem is. Can you generate data to plot and the only issue is getting multiple lines on one graph? Or are you having trouble generating data using your function? In either case, we can't help at all without your function (or a suitable example function), some details about the range... and of course seeing your attempt to know where we need to start explaining. – Gregor Thomas Dec 19 '17 at 02:21
  • Echoing what others have said: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Woodstock Dec 19 '17 at 05:04

1 Answers1

4

Say we have a simple function with two arguments:

fun <- function(x, y) 0.5*x - 0.01*x^2 + sqrt(abs(y)/2)

And we want to evaluate it on the following x and y values:

xs <- seq(-100, 100, by=1)
ys <- c(0, 100, 300)

This line below might be a bit hard to understand but it does all of the work:

res <- mapply(fun, list(xs), ys)

mapply allows us to run function with multiple variables across a range of values. Here we provide it with only one value for "x" argument (note that xs is a long vector, but since it is in a list - it's only one instance). We also provide multiple values of "y" argument. So the function will run 3 times each with the same value of x and different values of y.

Results are arranged column-wise so in the end we have 3 columns. Now we only have to plot:

cols <- c("black", "cornflowerblue", "orange")
matplot(xs, res, col=cols, type="l", lty=1, lwd=2, xlab="x", ylab="result")
legend("bottomright", legend=ys, title="value of y", lwd=2, col=cols)

Here the matplot function does all the work - it plots a line for every column in the provided matrix. Everything else is decoration.

Here is the result:

enter image description here

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89