R 如何获得垂直几何线到课堂日期的x轴?

R 如何获得垂直几何线到课堂日期的x轴?,r,date,ggplot2,time-series,R,Date,Ggplot2,Time Series,尽管我在谷歌组的POSIXct和geom_vline上找到了哈德利的帖子,但我还是无法完成。我有一个时间序列,从1998年到2005年,再到2010年,我想画一条垂直线。我尝试了ggplot和qplot语法,但我仍然没有看到任何垂直线,或者垂直线是在第一个垂直网格处绘制的,整个系列有点奇怪地向右移动 gg <- ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) + layer(geom="line"

尽管我在谷歌组的
POSIXct
geom_vline
上找到了哈德利的帖子,但我还是无法完成。我有一个时间序列,从1998年到2005年,再到2010年,我想画一条垂直线。我尝试了
ggplot
qplot
语法,但我仍然没有看到任何垂直线,或者垂直线是在第一个垂直网格处绘制的,整个系列有点奇怪地向右移动

gg <- ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) +
      layer(geom="line")
gg + geom_vline(xintercept=mydata$datefield[120],linetype=4)
# returns just the time series plot I had before, 
# interestingly the legend contains dotted vertical lines
ggTry
as.numeric(mydata$datefield[120])

一个简单的测试示例:

library("ggplot2")

tmp <- data.frame(x=rep(seq(as.Date(0, origin="1970-01-01"),
                            length=36, by="1 month"), 2),
                  y=rnorm(72),
                  category=gl(2,36))

p <- ggplot(tmp, aes(x, y, colour=category)) +
     geom_line() +
     geom_vline(xintercept=as.numeric(tmp$x[c(13, 24)]),
                linetype=4, colour="black")
print(p)
库(“ggplot2”)

tmp如果您希望无论您的日期是否在第120行,该行都保持不变,您也可以使用
geom_vline(xintercept=as.numeric(as.Date(“2015-01-01”)),linetype=4)

as.numeric对我有用

ggplot(data=bmelt)+
  geom_line(aes(x=day,y=value,colour=type),size=0.9)+
  scale_color_manual(labels = c("Observed","Counterfactual"),values = c("1","2"))+
  geom_ribbon(data=ita3,aes(x=day, 
      y=expcumresponse, ymin=exp.cr.ll,ymax=exp.cr.uu),alpha=0.2) +
  labs(title="Italy Confirmed cases",
        y ="# Cases ", x = "Date",color="Output")+
  geom_vline(xintercept = as.numeric(ymd("2020-03-13")), linetype="dashed", 
                color = "blue", size=1.5)+
  theme_minimal()

根据您将“日期”列传递给aes的方式,
as.numeric
as.POSIXct
工作:

库(ggplot2)

  • 使用
    aes(如日期),…)

  • 使用aes(日期等)

  • 您可以添加几行数据帧,以便我们可以尝试您的示例吗?我发现'as.numeric()'实际上很难找到!谢谢。我想知道
    geom_vline(aes(xintercept=as.numeric(x[c(13,24)]))、linetype=4、color=“black”)
    是否会更习惯,即使用
    aes
    而不是
    tmp$
    。这种解决方案不再有效。代码生成“错误:试图创建没有stat的层。运行rlang::last_Error()查看错误发生的位置。``在我的机器上(带有R3.2.2和ggplot 1.0.1的Win10),我必须强制日期为POSIXct以使其正确对齐:as.POSIXct(as.date(c(“2016-12-01”,“2017-02-01”)))谢谢Jthorpe。。这是唯一对我有效的版本
    ggplot(data=bmelt)+
      geom_line(aes(x=day,y=value,colour=type),size=0.9)+
      scale_color_manual(labels = c("Observed","Counterfactual"),values = c("1","2"))+
      geom_ribbon(data=ita3,aes(x=day, 
          y=expcumresponse, ymin=exp.cr.ll,ymax=exp.cr.uu),alpha=0.2) +
      labs(title="Italy Confirmed cases",
            y ="# Cases ", x = "Date",color="Output")+
      geom_vline(xintercept = as.numeric(ymd("2020-03-13")), linetype="dashed", 
                    color = "blue", size=1.5)+
      theme_minimal()
    
    ggplot(df, aes(as.Date(Dates), value)) +
       geom_line() +
       geom_vline(xintercept = as.numeric(as.Date("2020-11-20")), 
                  color = "red", 
                  lwd = 2) 
    
    ggplot(df, aes(Dates, value)) +
       geom_line() +
       geom_vline(xintercept = as.POSIXct(as.Date("2020-11-20")), 
                  color = "red", 
                  lwd = 2)