if else condition in ggplot to add an extra layer

前端 未结 4 585

say I want to plot two layers in ggplot, one containing points and another one containing lines if a certain criteria is fulfilled.

The code without the criteria cou

4条回答
  •  青春惊慌失措
    2020-11-30 09:11

    What you are seeing is a syntax error. The most robust way I can think of is:

    tmp.data<-c(1,2,3) 
    if(tmp.data[1]!="no value") {
       p = p + geom_point()
    }
    p + geom_line()
    

    So you compose the object p in a sequence, only adding geom_point() when the if statements yields TRUE.

提交回复
热议问题