【发布时间】:2013-02-21 10:46:12
【问题描述】:
我在为 gplot2 图形添加垂直线时遇到了一些问题。
下面列出了我的示例数据框。
set.seed(1234)
df <- data.frame(Date=seq(as.POSIXct("05:00", format="%H:%M"),
as.POSIXct("23:00", format="%H:%M"), by="hours"))
df$Counts <- sample(19)
df <- df[-c(4,7,17,18),]
# generate the groups automatically and plot
idx <- c(1, diff(df$Date))
i2 <- c(1,which(idx != 1), nrow(df)+1)
df$grp <- rep(1:length(diff(i2)), diff(i2))
g <- ggplot(df, aes(x=Date, y=Counts)) + geom_line(aes(group = grp)) +
geom_point()
似乎有很多关于堆栈溢出和网络的讨论,通常是关于在时间序列上使用 vlines。我已经尝试纠正我的代码,但到目前为止我运气不佳。
例如,我想要 21 日下午 2 点的垂直线。
g1 <- g + geom_vline(xintercept=as.numeric(as.Date("2013-02-21 14:00:00")))
谁能告诉我如何让它工作?
【问题讨论】:
-
as.Date仅保留日期部分,而不是%H:%M:%S信息。