即使我发现哈德利对谷歌的论坛中发帖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
Run Code Online (Sandbox Code Playgroud)
我的日期字段格式为"1993-07-01",属于班级Date.
请看下面的例子
library(dplyr)
library(lubridate)
library(ggplot2)
data <- data_frame(time = c(ymd(20160201),
ymd(20160202),
ymd(20160203),
ymd(20160201),
ymd(20160202)),
value = c(1,1,1,2,2),
group = c('A','A','B','B','B'))
events <- data_frame(time = c(ymd(20160201),
ymd(20160202)),
text = c('who let the dogs out?',
'who? who? who?'))
ggplot(data, aes(x = time, y = value, group = group, color = group)) +
geom_line(size = 2 ) +
geom_vline(data = events, aes(xintercept = as.numeric(time)))
> data
# A tibble: 5 × 3
time value group
<date> <dbl> <chr>
1 2016-02-01 1 A
2 2016-02-02 …Run Code Online (Sandbox Code Playgroud)