Connecting across missing values with geom_line

后端 未结 2 949
盖世英雄少女心
盖世英雄少女心 2020-11-28 08:34

I\'m trying to figure out if it\'s possible to connect across missing values using geom_line. For example, in the link below there are missing values at time 3 in facet F. I

2条回答
  •  醉梦人生
    2020-11-28 09:07

    Richie's answer is very thorough, but I wanted to show something simpler. Since lines are not drawn to NA points, another approach is drop these points when drawing lines. This implicitly makes a linear interpolation between points (as straight lines do).

    Using dfr from Richie's answer, without needing the calculation of z step:

    ggplot(dfr, aes(x,y)) + 
      geom_point() +
      geom_line(data=dfr[!is.na(dfr$y),])
    

    For that matter, in this case the subsetting could be done for the whole thing.

    ggplot(dfr[!is.na(dfr$y),], aes(x,y)) + 
      geom_point() +
      geom_line()
    

提交回复
热议问题