Plot a Function with Several Arguments in R

Plot a function with several arguments in R

I'd do it with plyr and ggplot2,

Sample Image

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()

How to graph a function with multiple parameters

Curve

As I already mentioned in my comment I don't understand why you want to use curve since it is build to accept a single range of numbers e.g. 1,2,3,4 and then applies a formula to it. This makes plotting straightforward since the input numbers form the x-axis (thus 1,2,3,4...) and the formula results are plotted on the y-axis. In your case you don't have a single input range (actually you have four ranges: x1, x2, x3 and x4) and you cannot plot in 5D thus you have to pick one of x1:x4 to display on the x-axis. Also you have 2 * x2 * x4 + x3 ( x1 - 2 * x4) as formula however .. x3 ( .. cannot be directly interpreted by R therefore I added * to make 2 * x2 * x4 + x3 * ( x1 - 2 * x4) however if you meant that X3 should be multiplied with the numbers within brackets you should edit the formula in the code below.

Alternative
As you mentioned "x1 x2 x3 and x4 are all supposed to have different minimum and maximum values as well" I would suggest to simply write your own plotting function, like so (I now displayed x1 on the x-axis):

range.plot <- function( func, x1.range, x2.range, x3.range, x4.range ) {
# Apply formula
res <- data.frame(x = x1.range, y = func(x1.range, x2.range, x3.range, x4.range))

# Plot using ggplot2
ggplot(res, aes(x, y)) + geom_line()
}

fun <- function(x1,x2,x3,x4){2 * x2 * x4 + x3 * ( x1 - 2 * x4)} # note the added *
range.plot(fun, seq(1,10), seq(10,29), seq(5,14), seq(1,10))

Which will produce this plot:

Sample Image

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

For future readers, I am sharing @G5W 's comment as answer in here:

plot(function(x) { my.function(1,0,x) })

I believe this is the best way of those provided in here

How can I plot functions with varying parameters in one plot?

It's not the smartest solution, but it could get you there.

   plot(f(1,1:1000,y[1]), col=colors[1], type="l")
lines(f(1,1:1000,y[2]), col=colors[2])
lines(f(1,1:1000,y[3]), col=colors[3])
lines(f(1,1:1000,y[4]), col=colors[4])
lines(f(1,1:1000,y[5]), col=colors[5])
lines(f(1,1:1000,y[6]), col=colors[6])
lines(f(1,1:1000,y[7]), col=colors[7])

a start, plot
if you want to use ggplot2 you take a look at these examples.

ggplot2 plot function with several arguments

I would do this outside of ggplot2. I think it might be too much to expect ggplot to vectorise over two different parameters ...

This is with tidyverse, but could easily be done with apply as well.

 library(dplyr)
change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
dd <- expand.grid(d=0:1000,p=0:100)
dd %>% rowwise %>%
mutate(c=change(score=1,d=d,k=100,p=p)) ->
dd2

library(ggplot2)
ggp <- ggplot(dd2,aes(d,c,col=p,group=p))+
geom_path()

I only did p from 0 to 100 (rather than 0 to 1000) because 1 million points is a fairly large data set for ggplot. (Do you really need to see 1000 separate values? Maybe seq(0,1000,length=100) ?

Plotting a function with points()

You can use the plot function twice and add add = TRUE for the second plot.

With plot, you can also use from and to parameters to avoid repeating the y-axis limits, although it will keep the y-axis limits defined in the first plot (so it might not be optimal).

plot(function(x){x}, 
xlab="Celsius", xlim=c(-100, 100),
ylab="Degrees", ylim=c(-100, 100))
plot(function(x) {(x-32)*5/9}, from = -100, to = 100, typ="l", col="red", add=T)

example

As mentioned by @Roland and @user2554330, you can also use curves if you want to plot multiple lines from the same function, and use () to avoid assigning the function beforehand, with add = i!=1 standing for add = T at every iteration except the first one.

for(y in 1:10) {
curve((x + 10*y), from=-100, to=100, add=i!=1)
}

Sample Image

Apply function with multiple parameters and output variable

As @Greg mentions, the purpose of this code isn't clear. However, the question seems to relate to how apply() works so here goes:

Basically, when any of the apply family of functions is used, the user-enetered function (f(), in this case) is applied to the subset of the data produced by apply. So here, you've asked apply to evaluate each row then call f() - the first argument to f() would then be a vector rather than the data frame your function requires.

Here's some functioning code:

col_1 <- c("A", "B", "C")
col_2 <- c("red", "blue", "black")
df <- data.frame(col_1, col_2)
f <- function(x) {
x[1]
x[2]
}
apply(df, 1, f)

This generates all of the values of the second column as a vector since x[2] is returned from the function and for each row, will represent the value in the second column.

If you want the arg1 row of results, you could simply use the following:

find_row <- function(df, row) {
df[row, ]
}
find_row(df, 1)

apply() isn't required. Using a single function makes the code simpler to read and should be faster too.

Plot curve with multiple parameters

I guess you can use dbinom directly

curve(dbinom(5, 10, y), xname = "y")

Sample Image

or if you need to vary x, you can try

sapply(0:10, function(k) curve(dbinom(k, 10, y), xname = "y", add = TRUE, ylim = c(0, 1)))

Sample Image



Related Topics



Leave a reply



Submit