2

I will be highly grateful for your help. I am learning how to use geom_function in r. Following is my function:

x0 <- 0.5
x1 <- 1
x2 <- 2
x3 <- 3

x <- c(x0, x1, x2, x3)

myfn <- function(w, b, a, x){
    w^(1-b)/(1-b)-a*w-(w>100 & w<=200)*x[3]*(w-100)-(w>200)*x[3]*100-x[4]*(w-200)
}

My objective is to plot above function using geom_function to see how this function behaves with different values of arguments a and b and following is my code:

y=seq(0,1000,5)

ggplot()+
  xlim(c(0,1000))+
  geom_function(fun=myfn(w=y, b=-4, a=0.5, x=x))

Problem: I feel my logic is correct but when I execute above code, I get nothing. I will be highly grateful for the help. Thank you very much for the help in advance. Any help or direction will be highly appreciated.

2
  • you have not specified x in your aes Commented Jan 25, 2022 at 7:58
  • @Oliver: Thank you for your suggestion. I am confused, can you elaborate please. Commented Jan 25, 2022 at 8:07

2 Answers 2

3

Your function myfn is a function of w where a, b and x are parameters. To plot this function over the range of c(0, 1000) pass your function to the fun argument and the parameters as a list via the args argument and set the range via xlim:

x0 <- 0.5
x1 <- 1
x2 <- 2
x3 <- 3

x <- c(x0, x1, x2, x3)

myfn <- function(w, b, a, x) {
  w^(1 - b) / (1 - b) - a * w - (w > 100 & w <= 200) * x[3] * (w - 100) - (w > 200) * x[3] * 100 - x[4] * (w - 200)
}

library(ggplot2)

ggplot() +
  xlim(c(0, 1000)) +
  geom_function(fun = myfn, args = list(b = -4, a = 0.5, x = x))

A second option would be to make use of a lambda function like so:

ggplot() +
  xlim(c(0, 1000)) +
  geom_function(fun = ~ myfn(.x, b = -4, a = 0.5, x = x))

Sign up to request clarification or add additional context in comments.

3 Comments

Nice! I had set w equal to x and that did not work. I could not wrap my head around it. Does it consider w as y, because w it is left undefined?
It is a bit confusing, w is considered x in the graph, your x is just some constants and the y in the plot is the outcome of your function based on values for w.
@DionGroothof. Haha. Took me also some time to realize that x is not the argument but a parameter. But you are right. Under the hood geom_functionwill first create a grid according to the range. This vector is then passed to the function and will be passed to the first argument or the first unnamed argument, i.e. w. However we could also make use of a lambda function i.e. fun = ~ myfn(w = .x, b = -4, a = 0.5, x = x)
2
myfn <- function(x, a, b, c) {
  x^(1 - b) / (1 - b) - a * x - (x > 100 & x <= 200) * c[3] * (x - 100) - (x > 200) * c[3] * 100 - c[4] * (x - 200) # outcome is y
}

ggplot() +
  xlim(c(0, 1000)) +
  geom_function(fun = ~ myfn(x = .x, a = 0.5, b = -4, c = c(0.5, 1, 2, 3)))

If you do not want to add the variables through the args list you can add the variables to your function like this. Note I changed some of the variable names to make it more clear what the actual x and y are in the plot. Also x by the OP is just a list with 4 constants, I provided them as a and b under the name c.

Comments

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.