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


in experiment, blood pressure measured @ several time points. blood pressure rises , declines during experiment. need plot blood pressure reaction (the easy part) , find time points (x values) blood pressure has doubled (the tricky part). wondering whether information retrieved in ggplot?

here example:

# generate data time <- c(10, 60, 90, 200, 260, 300, 700) value <- c(1, 6, 8, 40, 50, 60, 70) df <- data.frame(time, value)  # first value of "value" first observation. # when first "value" increased ten times, equal 10 # question @ time point did value increase ten times according graph?  ggplot(data=c, aes(x=time, y=value,)) +       geom_line() +      geom_hline(y=10, colour="red") +      annotate("text", hjust=0, x=170, y=15, label="i need find x value @ intersection") 

enter image description here

any solutions?

no, can't done ggplot2. however, it's easy do:

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

if data bijective gets simpler:

f2 <- approxfun(df$value, df$time) f2(v0) #[1] 96.875 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -