2015-10-19 132 views
2

我正在使用ggplot2來繪製我的每小時時間序列數據。數據組織是使用ggplot2繪製每小時的數據

> head(df) 
      timestamp power 
1 2015-08-01 00:00:00 584.4069 
2 2015-08-01 01:00:00 577.2829 
3 2015-08-01 02:00:00 569.0937 
4 2015-08-01 03:00:00 561.6945 
5 2015-08-01 04:00:00 557.9449 
6 2015-08-01 05:00:00 562.4152 

我用以下GGPLOT2命令繪製數據:

ggplot(df,aes(timestamp,power,group=1))+ theme_bw() + geom_line()+ 
    scale_x_datetime(labels = date_format("%d:%m; %H"), breaks=pretty_breaks(n=30)) + 
     theme(axis.text.x = element_text(angle=90,hjust=1)) 

與此繪製的圖是: enter image description here

我的問題是:

  • 在繪製圖中,爲什麼它只顯示與相對應的標籤。現在,如果我想顯示對應於每天hour 12的標籤,該怎麼辦。
  • 我正在繪製每小時的數據,希望看到細粒度的細節。但是,我無法看到整整一個月的所有時間。我能以某種方式在同一圖表中查看任何選定日期的縮放視圖嗎?

回答

2

這是一個在ggplot中縮放日期的相當長的例子,也是一個可能的交互方式來放大範圍。首先,一些示例數據,

## Make some sample data 
library(zoo) # rollmean 
set.seed(0) 
n <- 745 
x <- rgamma(n,.15)*abs(sin(1:n*pi*24/n))*sin(1:n*pi/n/5) 
x <- rollmean(x, 3, 0) 

start.date <- as.POSIXct('2015-08-01 00:00:00') # the min from your df 
dat <- data.frame(
    timestamp=as.POSIXct(seq.POSIXt(start.date, start.date + 60*60*24*31, by="hour")), 
    power=x * 3000) 

對於交互式縮放,您可以嘗試plotly。你需要設置它(得到一個API密鑰和用戶名)然後就去做

library(plotly) 
plot_ly(dat, x=timestamp, y=power, text=power, type='line') 

,您可以選擇圖形的區域和放大它們。你可以看到它here

要改變ggplot圖中的中斷,這裏有一個函數可以在特定的時間以不同的間隔進行日期中斷。

## Make breaks from a starting date at a given hour, occuring by interval, 
## length.out is days 
make_breaks <- function(strt, hour, interval="day", length.out=31) { 
    strt <- as.POSIXlt(strt - 60*60*24) # start back one day 
    strt <- ISOdatetime(strt$year+1900L, strt$mon+1L, strt$mday, hour=hour, min=0, sec=0, tz="UTC") 
    seq.POSIXt(strt, strt+(1+length.out)*60*60*24, by=interval) 
} 

一種方式來放大,非交互,是簡單的子集數據,

library(scales) 
library(ggplot2) 
library(gridExtra) 

## The whole interval, breaks on hour 18 each day 
breaks <- make_breaks(min(dat$timestamp), hour=18, interval="day", length.out=31) 
p1 <- ggplot(dat,aes(timestamp,power,group=1))+ theme_bw() + geom_line()+ 
    scale_x_datetime(labels = date_format("%d:%m; %H"), breaks=breaks) + 
    theme(axis.text.x = element_text(angle=90,hjust=1)) + 
    ggtitle("Full Range") 

## Look at a specific day, breaks by hour 
days <- 20 
samp <- dat[format(dat$timestamp, "%d") %in% as.character(days),] 
breaks <- make_breaks(min(samp$timestamp), hour=0, interval='hour', length.out=length(days)) 
p2 <- ggplot(samp,aes(timestamp,power,group=1))+ theme_bw() + geom_line()+ 
    scale_x_datetime(labels = date_format("%d:%m; %H"), breaks=breaks) + 
    theme(axis.text.x = element_text(angle=90,hjust=1)) + 
    ggtitle(paste("Day:", paste(days, collapse = ", "))) 

grid.arrange(p1, p2) 

enter image description here

1

我沒有數據時,數據的工作很多,所以我的代碼看起來有點凌亂......但是解決1是使用pretty_breaks()但更好地利用混凝土斷裂,也限制了內功能scale_x_datetime()

一個爛筆頭例子可能是以下幾點:

ggplot(df,aes(timestamp,power,group=1))+ theme_bw() + geom_line()+ 
    scale_x_datetime(labels = date_format("%d:%m; %H"), 
        breaks=as.POSIXct(sapply(seq(18000, 3600000, 86400), function(x) 0 + x), 
            origin="2015-10-19 7:00:00"), 
        limits=c(as.POSIXct(3000, origin="2015-10-19 7:00:00"), 
          as.POSIXct(30000, origin="2015-10-19 7:00:00"))) + 
    theme(axis.text.x = element_text(angle=90,hjust=1)) 

我不知道該怎麼寫as.POSIXct()更具可讀性......但基本上手動創建的12個時針位置和內隨時添加一個完整的一天你的數據幀的範圍...