简体   繁体   English

通过在数据框中使用标志添加垂直线

[英]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值的索引。我们提取这些索引,并使用minTIME的最小值中选择一个。 Finally using this index we pick the correponding value from df$TIME . 最后,使用该索引,我们从df$TIME选择相应的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM