[英]Adding a vertical line by using a flag in the data frame
I have a data frame as follows: 我有一个数据框,如下所示:
df<-
FORM TIME CONC
0 1 5
0 2 10
0 5 11
1 6 14
1 7 15
I am plotting TIME
versus CONC
. 我正在绘制
TIME
vs CONC
。 I would like to add a vertical line at the TIME
when the formulation changes from FORM 1
to FORM 2
. 当配方从
FORM 1
更改为FORM 2
时,我想在TIME
添加一条垂直线。 I want to make my code for plotting so it can detect when the formulation changes automatically. 我想制作我的绘图代码,以便它可以检测配方何时自动更改。
How should I write it in the ggplot line below: 我应该如何在下面的ggplot行中编写它:
plotobj <- plotobj + vline(aes(slope = 1, intercept = ??), linetype = "dashed", size = 1)
If I read you correctly, this should help out: 如果我没看错,这应该可以帮助您:
library(ggplot2) 库(GGPLOT2)
df = data.frame(FORM=c(0,0,0,1,1),TIME=c(1,2,5,6,7),CONC=c(5,10,11,14,15))
ggplot()+
geom_line(data=df,aes(x=TIME,y=CONC))+
geom_vline(xintercept = min(df$TIME[grep(1,df$FORM)]), linetype = "dashed", size = 1)
What is actually important: 实际重要的是:
min(df$TIME[grep(1,df$FORM)])
grep
gives indexes for all FORM
values equal to 1. We extract these and chose the one from the row with the smallest value of TIME
using min
. grep
给出所有等于1的FORM
值的索引。我们提取这些索引,并使用min
从TIME
的最小值中选择一个。 Finally using this index we pick the correponding value from df$TIME
. 最后,使用该索引,我们从
df$TIME
选择相应的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.