如何获得班级日期的 x 轴的垂直 geom_vline ?

发布于 2024-10-25 22:19:14 字数 560 浏览 5 评论 0原文

尽管我在 POSIXctgeom_vline 的 google 群组中找到了 Hadley 的帖子,但我无法完成它。我有一个时间序列,想画一条垂直线,例如 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

我的日期字段的格式为“1993-07-01”,属于 Date 类。

Even though I found Hadley's post in the google group on POSIXct and geom_vline, I could not get it done. I have a time series from and would like to draw a vertical line for years 1998, 2005 and 2010 for example. I tried with ggplot and qplot syntax, but still I either see no vertical line at all or the vertical line is drawn at the very first vertical grid and the whole series is shifted somewhat strangely to the right.

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

My date field has format "1993-07-01" and is of class Date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满栀 2024-11-01 22:19:14

尝试 as.numeric(mydata$datefield[120])

gg + geom_vline(xintercept=as.numeric(mydata$datefield[120]), linetype=4)

一个简单的测试示例:

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)

Try as.numeric(mydata$datefield[120]):

gg + geom_vline(xintercept=as.numeric(mydata$datefield[120]), linetype=4)

A simple test example:

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)

geom_vline example plot

家住魔仙堡 2024-11-01 22:19:14

如果您希望线条保持在原位,无论您是否日期位于第 120 行。

You could also do geom_vline(xintercept = as.numeric(as.Date("2015-01-01")), linetype=4) if you want the line to stay in place whether or not your date is in the 120th row.

真心难拥有 2024-11-01 22:19:14

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

library(ggplot2 )

  1. 使用aes(as.Date(Dates),...)

    ggplot(df, aes(as.Date(日期), value)) +
       几何线()+
       geom_vline(xintercept = as.numeric(as.Date("2020-11-20")), 
                  颜色=“红色”, 
                  长距离 = 2) 
    
  2. 使用aes(Dates, ...)

    ggplot(df, aes(日期, 值)) +
       几何线()+
       geom_vline(xintercept = as.POSIXct(as.Date("2020-11-20")), 
                  颜色=“红色”, 
                  长距离 = 2) 
    

Depending on how you pass your "Dates" column to aes, either as.numeric or as.POSIXct works:

library(ggplot2)

  1. using aes(as.Date(Dates),...)

    ggplot(df, aes(as.Date(Dates), value)) +
       geom_line() +
       geom_vline(xintercept = as.numeric(as.Date("2020-11-20")), 
                  color = "red", 
                  lwd = 2) 
    
  2. using aes(Dates, ...)

    ggplot(df, aes(Dates, value)) +
       geom_line() +
       geom_vline(xintercept = as.POSIXct(as.Date("2020-11-20")), 
                  color = "red", 
                  lwd = 2) 
    
盗梦空间 2024-11-01 22:19:14

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()

在此处输入图像描述

as.numeric works to me

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文