在实验中,在几个时间点测量血压。实验期间血压上升和下降。我需要绘制血压反应(简单部分)并找出血压加倍的时间点(x值)(棘手的部分)。我想知道是否可以在ggplot中检索这些信息?
以下是一个例子:
# 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)
# The first value of "value" is the first observation.
# When the first "value" increased ten times, it is equal to 10
# Question is at what time point did the value increase ten times according to the 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 to find the x value at the intersection")
任何解决方案?
答案 0 :(得分:5)
不,这不能用ggplot2完成。但是,这很容易做到:
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
如果你的数据是双射的,那就更简单了:
f2 <- approxfun(df$value, df$time)
f2(v0)
#[1] 96.875