I am trying to plot a graph of two functions (i.e., 2x + 1 and 3x - 0.5*x^2) on a same graph sheet, but with two different ranges (x<=3 and x>3, respectively). So far, I have defined two functions as the following:
# Define first function with given range
Line1 <- function(x) {
if (x <= 3) {
(3*x) + 1 } else {
}
}
# Define second function with given range
Line2 <- function(x) {
if (x > 3) {
2*x - (0.5*(x^2)) } else {
}
}
# Plot functions
ggplot(d,aes(x=x)) + stat_function(fun=rwrap(Line1,0,3),geom="line",col="blue")
+ stat_function(fun=rwrap(Line2,3,5),geom="line",col="red")`
When I run the code, I get the following error messages:
Error: Discrete value supplied to continuous scale In addition:
Warning messages:
1: In if (x <= 3) { : the condition has length > 1 and only the first element will be used
2: In if (x > 3) { : the condition has length > 1 and only the first element will be used
I tried to troubleshoot it, but I am just lost at this point.