2016-04-03 56 views
1

我只想在每天上午7点到上午9点的时间序列数据集中查看趋势(即Y)。ggplot:根据日期设置刻面绘图的时间范围

你知道如何将它绘制在ggplot中吗?

ggplot方面的每个子图显示每天上午7点到上午9点(2010/1/1到2010/1/14)的趋势Y.

这里是我的数据集:

DateTime <- seq(as.POSIXct("2010/1/1 00:00"), as.POSIXct("2010/1/15 00:00"), "min") 
    Y <- rnorm(n=length(DateTime), mean=100, sd=1) 
    df <- data.frame(DateTime, Y) 

非常感谢!

回答

3
require(ggplot2) 
df <- dplyr::filter(df, format(DateTime, '%H:%M:%S') < '09:00:00', format(DateTime, '%H:%M:%S') > '07:00:00') 

df$Day <- format(df$DateTime, '%d') 

plt <- ggplot(df, aes(DateTime, Y)) + geom_line() + facet_wrap(~Day, ncol = 2, scales = 'free_x') + 
theme(axis.text.x = element_text(size = rel(0.7))) 

print(plt) 

Output Image

+0

是从''filter' dplyr'? – Axeman

+1

是的,我很抱歉我没有提到。 – TheRimalaya

+0

我已经更正了现在的答案。 – TheRimalaya