2015-10-19 58 views
0

我有三個月的時間序列數據箱線圖:繪製時間序列數據,使用GGPLOT2

structure(list(timestamp = structure(c(1438367400, 1438453800, 
1438540200, 1438626600, 1438713000, 1438799400, 1438885800, 1438972200, 
1439058600, 1439145000, 1439231400, 1439317800, 1439404200, 1439490600, 
1439577000, 1439663400, 1439749800, 1439836200, 1439922600, 1440009000, 
1440095400, 1440181800, 1440268200, 1440354600, 1440441000, 1440527400, 
1440613800, 1440700200, 1440786600, 1440873000, 1440959400, 1441045800, 
1441132200, 1441218600, 1441305000, 1441391400, 1441477800, 1441564200, 
1441650600, 1441737000, 1441823400, 1441909800, 1441996200, 1442082600, 
1442169000, 1442255400, 1442341800, 1442428200, 1442514600, 1442601000, 
1442687400, 1442773800, 1442860200, 1442946600, 1443033000, 1443119400, 
1443205800, 1443292200, 1443378600, 1443465000, 1443551400, 1443637800, 
1443724200, 1443810600, 1443897000, 1443983400, 1444069800, 1444156200, 
1444242600, 1444329000, 1444415400, 1444501800, 1444588200, 1444674600, 
1444761000, 1444847400, 1444933800, 1445020200, 1445106600, 1445193000, 
1445279400, 1445365800, 1445452200, 1445538600, 1445625000, 1445711400, 
1445797800, 1445884200, 1445970600, 1446057000, 1446143400, 1446229800 
), class = c("POSIXct", "POSIXt"), tzone = "Asia/Kolkata"), power = c(0.0247230720457733, 
0.0108825766825672, 0.0288152903005685, -0.0234123772463031, 
-0.00919754217224866, -0.0374403275019726, -0.0078777961550861, 
0.0079837844138577, 0.00575982953501377, 0.0202857581215497, 
0.0258757511850728, -0.0337682832455853, 0.0341912141620086, 
-0.0042798060711745, 0.00675858841374106, -0.00246125938541154, 
0.00912344869950909, 0.0206439842553702, -0.01940932272705, -0.0100830115181366, 
-0.00734857221326485, -0.0170222915573808, 0.0285240366773044, 
0.00764357383266427, -0.0238453527011212, -0.00672944465931788, 
0.00818204423502334, 0.00460125531472914, 0.0230428947908782, 
0.00154030144193071, 0.0189660255911821, 0.0155756949832998, 
0.0267515034089843, 0.0214963580786819, 0.0029481279523321, 0.00986682946599846, 
0.00832138782007834, -0.00676599971272203, -0.00324283490587677, 
0.0204023688477303, 0.0229717200642572, 0.0251381446922004, 0.00584010113018711, 
-0.00465215879879816, -0.0308844467014504, 0.000780093550060347, 
0.00369764046959574, -0.0160883189684658, 0.0218083597791737, 
-0.021605117962637, 0.000445192082904827, -0.00372433762871899, 
0.0106455591452724, 0.024611532476291, 0.0132632680432167, 0.0149559186037772, 
-0.0453599092776512, -0.0202099060937128, 0.0169712680315599, 
0.0148844950106621, -0.0391221225281138, 0.00461340547288957, 
0.0118982098114901, -0.00571305945781934, 0.0143190640584365, 
-0.0117202800880833, 0.0394635775820876, -0.00393737330560111, 
0.00633578511802405, 0.0402779799675971, -0.00146576620678839, 
-0.00974562394885507, 0.0179401329290733, -0.0157766103469759, 
0.00534190906349559, 0.0021055773020779, -0.018236876969857, 
-0.00392926841238278, -0.0065097462318096, -0.0249870099671041, 
-0.0139735459289521, 0.000625022444854668, -0.00278827413472659, 
0.0179048032598685, -0.0268735489312987, -0.00760559474288195, 
0.0179536496832603, -0.0126341858034209, -0.0338553507687797, 
-0.0045297254037134, -0.0106755306681489, -0.0154881466662607 
)), .Names = c("timestamp", "power"), row.names = c(NA, -92L), class = "data.frame") 

給出的數據幀的結構是:

> str(h2) 
'data.frame': 92 obs. of 2 variables: 
$ timestamp: POSIXct, format: "2015-08-01" "2015-08-02" "2015-08-03" "2015-08-04" ... 
$ power : num 0.0247 0.0109 0.0288 -0.0234 -0.0092 ... 

現在,我想要使用ggplot2以boxlot的形式繪製這些數據。相信會有三個盒子,晶須 s對應三個月。直到現在,我正在使用以下代碼:

ggplot(dats) +geom_boxplot(aes(x=timestamp,y=power,group=timestamp),width = 0.5) + 
     theme(axis.text.x = element_text(angle=90,hjust=1)) + 
    scale_x_datetime(labels = date_format("%b "), breaks="1 month") 

我相信我無法按月正確分組數據。如果我是正確的,我應該如何將數據按月分組,以便繪製箱形圖?

注意:我已經爲這個問題提供了一個答案,但在答案中,我將時間戳從POSIXct轉換爲因子。但是,我想使用給定的POSIXct時間戳進行繪圖,因爲轉換過程會消耗一些額外的時間。

+0

假設時間戳已經轉換爲日期時間,請嘗試加載lubridate包並將時間戳轉換爲月份函數的月份。這將把時間戳轉換爲幾個月,並且你的代碼應該從那裏開始工作 – Chris

回答

0

注:本答案中使用的數據集比問題

指定在POSIXct形式對因子的轉換的一個完全不同的,下面的腳本可以使用:

#First convert timestamp to months format and use levels so that final output 
    #is presented in chronological order and not in alphabetical order 
    dats$timestamp <- factor(strftime(dats$timestamp,"%b"),levels = month.abb) 
    ggplot(dats) +geom_boxplot(aes(x=timestamp,y=power)) + 
     theme(axis.text.x = element_text(angle=90,hjust=1)) + xlab("Time Period") +ylab("Power") 

最後的情節是: enter image description here UPDATE 沒有時間轉換,即直接基於POSIXct時間戳,我這樣做是因爲

ggplot(dats) +geom_boxplot(aes(x=timestamp,y=power, group=months(timestamp)))+ 
    theme(axis.text.x = element_text(angle=90,hjust=1)) + xlab("Time Period") +ylab("Power") + 
    scale_x_datetime(breaks="1 month", labels = date_format("%b")) 

我想要使用POSIXct窗體的相同類型的陰謀。