我有一個我很滿意的區域圖。我試圖在面積圖的頂部覆蓋一條粗實線。在ggplot的區域圖中添加一條總和線
該圖是按渠道顯示的網站會話,其中每個渠道均爲面積圖中的組(填充)。我的想法是用一個非常沉重的阿爾法來顯示總會話的實線圖,顯示這些會話的來源。
通過數據是這樣的(因爲ggplot功能依賴於數據結構)
> str(dataset)
'data.frame': 144 obs. of 5 variables:
$ Month : Factor w/ 24 levels "May-2015","Jun-2015",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Channel : Factor w/ 6 levels "Facebook","Youtube",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Sessions : num 5065 4226 4779 5736 6350 ...
> head(dataset, n = 20)
Month Channel Sessions
1 May-2015 Facebook 5065
2 Jun-2015 Facebook 4226
3 Jul-2015 Facebook 4779
4 Aug-2015 Facebook 5736
5 Sep-2015 Facebook 6350
6 Oct-2015 Facebook 6199
7 Nov-2015 Facebook 8474
8 Dec-2015 Facebook 8340
9 Jan-2016 Facebook 11376
10 Feb-2016 Facebook 11290
11 Mar-2016 Facebook 13255
12 Apr-2016 Facebook 16693
13 May-2016 Facebook 14618
14 Jun-2016 Facebook 14208
15 Jul-2016 Facebook 14016
16 Aug-2016 Facebook 14978
17 Sep-2016 Facebook 14559
18 Oct-2016 Facebook 10583
19 Nov-2016 Facebook 6930
20 Dec-2016 Facebook 8918
我的面積圖:
timeline <- ggplot(dataset, aes(x = Month, y = Sessions,fill = Channel, group = Channel)) +
geom_area(alpha = 0.7) +
# This piece right here is where I tried to add a solid line
geom_line(data = dataset, inherit.aes = FALSE, aes(x = Month, y = Sessions, group = Month)) +
theme(axis.text.x=element_text(angle=90, hjust=1))
我成功了截至及包括geom_area(alpha = 0.7)
因爲這產生了很好的面積圖。
但接下來的行導致意外的行爲:
geom_line(data = dataset, inherit.aes = FALSE, aes(x = Month, y = Sessions, group = Month))
我希望看到覆蓋在上面絡繹不絕,而是取得了一系列的破碎豎線。查看輸出底部的黑線。我嘗試添加/刪除命令group = Month
但這並沒有改變任何東西:
如何通過沿着堆積區域圖最頂部的折線圖添加實線以表示總會話?
換句話說當前圖表的最頂部應該有一個堅實的粗線表示總交通量,因爲下面的區域堆積 –
@Axeman感謝,我給一個嘗試,但沒有改變'時間表< - ggplot(數據集,aes(x = Month,y = Sessions,fill = Channel,group = Channel))+ geom_area(alpha = 0.3)+ + stat_summary(aes(group = 1),fun.y = sum,geom =' line')' –
D'oh!我有兩個加號++,它可以工作。謝謝! –