I'm going to depict a continuous graph on R using ggplot2
library, which is based on piecewise function.
fun1 <- function(x){
ifelse(x<=-2, -1.5*(-x)^2, NA)
}
fun2 <- function(x){
ifelse(x>-2, 6*x-5, NA)
}
gg <- ggplot(data.frame(x = c(-5, 5)),
aes(x = x))+
stat_function(fun = fun1, n=1001)+
stat_function(fun = fun2, n=1001)
print(gg)
This shows the correct graph of the piecewise function.
But if I change the condition slightly different, such that ifelse(x<-2, -1.5*(-x)^2, NA)
and ifelse(x>=-2, 6*x-5, NA)
, then suddenly the first part of the graph would not be depicted correctly.
This seems that the ifelse
function now always returns -6
for all values x
, irrespective of if it is <=-2
or not.
But why does this change suddenly happen? I tried to depict a continuous curve using standard plot function but still got the same result...