图中的多个vlines给出了错误ggplot2

时间:2016-06-29 07:26:08

标签: r ggplot2 time-series

我想完全按照here解释,接受的答案因某种原因对我不起作用。

+---------+------------------------+-----------------------------+
|name     |Entrance                |Exit                         |
+---------+------------------------+-----------------------------+
|Maria    |2012-02-14 09:04:45.397 |2013-02-14 12:20:59.407      |
|Maria    |2012-02-13 12:14:20.997 |          null               |
|Eli      |2013-02-13 10:30:59.407 |2013-02-13 12:30:59.407      |
+---------+------------------------+-----------------------------+

我用geom_line()交换了图层(geom =“line”)以仅获得图表。但是,添加vlines时出现此错误:

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(aes(xintercept=as.numeric(x[c(13, 24)])),
                linetype=4, colour="black")
print(p)

1 个答案:

答案 0 :(得分:1)

以下为我工作:

ggplot(tmp,aes(x, y, colour=category)) +
geom_line() +
geom_vline(data=tmp[c(13,26),],aes(xintercept=as.numeric(x)))