Why if statement does not work inside ggplot function but works as a last line:
index <- 1
This works:
ggplot(mtcars, aes(wt, mpg))+
geom_line()+
if(index==1) geom_point()
However, when I move the if statement, it does not work:
ggplot(mtcars, aes(wt, mpg))+
if(index==1) geom_point()+ #or if(index==1) {geom_point()}+
geom_line()
Is there anyway to use if statement inside ggplot, not in the last line? I know I can do the following, but I am curious if I can use it inside ggplot:
p <- ggplot(mtcars, aes(wt, mpg))+
geom_line()
if(index==1) p <- p + geom_point()