2014-08-27 131 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