2014-08-27 79 views
-3

大家好我有許多因素,數據幀,然後許多數字域[R GGPLOT2聚集

的數據看起來像這樣

Date Source Device Sessions 2014-05-01 Email Desktop 245 2014-05-01 Facebook Desktop 132 2014-05-01 (not set) Desktop 1143 2014-05-01 Email Mobile 72 2014-05-01 Facebook Mobile 96 2014-05-02 Email Desktop 187 2014-05-02 Email Mobile 32 2014-05-02 Facebook Desktop 110

我想創造的ggplot2中的折線圖,其中包含沿X軸的日期並聚合要由線表示的會話總數。我似乎無法找到如何根據日期值聚合會話列中的數據。

我也希望能夠創建一個圖表,使用設備變量爲列中的每個值創建一行(例如,總結桌面會話和總結會話的行的行手機)仍然使用日期作爲X軸。

在此先感謝

+0

@丹:您的反饋將不勝感激。 – rnso 2014-08-27 14:04:25

回答

0

嘗試:

> tt2 = with(ddf, tapply(Sessions, list(Date,Device), sum)) 
> data.frame(tt2) 
      Desktop Mobile 
2014-05-01 1520 168 
2014-05-02  297  32 
> 
> dd2 = data.frame(tt2) 
> 
> dd2 
      Desktop Mobile 
2014-05-01 1520 168 
2014-05-02  297  32 
> 
> 
> 
> dd2$date = rownames(dd2) 
> dd2 
      Desktop Mobile  date 
2014-05-01 1520 168 2014-05-01 
2014-05-02  297  32 2014-05-02 
> 
> 
> 
> dd2m = melt(dd2, id='date') 
> 
> dd2m 
     date variable value 
1 2014-05-01 Desktop 1520 
2 2014-05-02 Desktop 297 
3 2014-05-01 Mobile 168 
4 2014-05-02 Mobile 32 
> 
ggplot(dd2m, aes(x=date, y=value, group=variable, color=variable))+geom_point()+geom_line() 

> 

enter image description here

+0

謝謝你的回答@mso它確實引發了我的一些想法,這讓我認爲,爲了繪製我想要的東西,我需要在繪圖之前按照日期彙總我的數據。爲此,我懷疑你的答案,當它到達那裏有一點冗長的囉嗦。 我用的是: 'datedf < - aggregate(sessions〜date,FUN = sum,data = df); ggplot(datedf,aes(x = date,y = sessions))+ geom_line()+ geom_point()' – Dan 2014-08-28 10:11:16