R ggplot2能找到交叉点吗?或者有其他整洁的方法吗?
在一项实验中,血压是在几个时间点测量的。在实验过程中,血压有升有降。我需要绘制血压反应图(容易的部分)并找到血压加倍的时间点(x值)(棘手的部分)。我想知道这个信息是否可以在ggplot中检索到 以下是一个例子:R ggplot2能找到交叉点吗?或者有其他整洁的方法吗?,r,ggplot2,R,Ggplot2,在一项实验中,血压是在几个时间点测量的。在实验过程中,血压有升有降。我需要绘制血压反应图(容易的部分)并找到血压加倍的时间点(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 fir
# 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")
#生成数据
时间否,这不能用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
v0不,这不能用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
v0这是一个漂亮的解决方案!我在我的数据集上试过了。验证它有一些困难,我想知道如果第一个时间变量为负,这个函数是否能够正确地进行计算。因为第一次测量是在给药前5分钟(时间=负5)进行的。所以时间变量从负5分钟变为正120分钟。计算结果是否仍然正确,还是必须为每个时间值添加一个常量?非常感谢approxfun
只需在数据点之间插值即可。它不在乎这些值是正值还是负值。绘制结果以验证它。我无法让函数使用以下数据集,它会产生不可信的值。我已经试了几个小时来解决这个问题,但我不知道我做错了什么。请注意,我的目标是分别估计由称为“type”的二分法变量描述的两个组的值。数据:这是一个漂亮的解决方案!我在我的数据集上试过了。验证它有一些困难,我想知道如果第一个时间变量为负,这个函数是否能够正确地进行计算。因为第一次测量是在给药前5分钟(时间=负5)进行的。所以时间变量从负5分钟变为正120分钟。计算结果是否仍然正确,还是必须为每个时间值添加一个常量?非常感谢approxfun
只需在数据点之间插值即可。它不在乎这些值是正值还是负值。绘制结果以验证它。我无法让函数使用以下数据集,它会产生不可信的值。我已经试了几个小时来解决这个问题,但我不知道我做错了什么。请注意,我的目标是分别估计由称为“type”的二分法变量描述的两个组的值。数据: