2016-04-03 89 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