Can ggplot2 find the intersections - or is there any other neat way?

后端 未结 1 437
粉色の甜心
粉色の甜心 2020-12-11 23:01

In an experiment, blood pressure is measured at several time points. Blood pressure rises and declines during the experiment. I need to plot the blood pressure reaction (the

相关标签:
1条回答
  • 2020-12-11 23:08

    No, this can't be done with ggplot2. However, it's easy to do:

    v0 <- 10
    
    f1 <- approxfun(df$time, df$value)
    
    #we use numeric optimization here, but an analytical solution is of course possible (though a bit more work)
    #this finds only one intersection, more work is required if there are more than one
    optimize(function(t0) abs(f1(t0) - v0), interval = range(df$time))
    #$minimum
    #[1] 96.87501
    #
    #$objective
    #[1] 3.080161e-06
    

    If your data is bijective it gets even simpler:

    f2 <- approxfun(df$value, df$time)
    f2(v0)
    #[1] 96.875
    
    0 讨论(0)
提交回复
热议问题