2017-06-21 8 views
0

時間の経過とともにサイトの平均時間をプロットすることを検討しています。私のデータセットはAPRAと呼ばれ、POSIXctという日付とVisit_Time_Per_Page_(Minutes)という列がnum形式であるPost_Dayという列があります。ggplot2で時間とともに平均値をプロットする方法R

私は、この入力すると:

ggplot(APRA,aes(Post_Day,mean(`Visit_Time_Per_Page_(Minutes)`)))+ 
    geom_line()+ 
    labs(title = "Time on Page over Time", x = "Date", y = "Time on Page (Minutes)") 

を、私はこれを取り戻す:

enter image description here

私は後だ、何が毎日平均は、時間にわたってプロットです。

ありがとうございました。データの

サンプル:

Post_Title Post_Day Visit_Time_Per_Page_(Minutes) 
Title 1  2016-05-15 4.7 
Title 2  2016-05-15 3.8 
Title 3  2016-05-15 5.3 
Title 4  2016-05-16 2.9 
Title 5  2016-05-17 5.0 
Title 6  2017-05-17 4.3 
Title 7  2017-05-17 4.7 
Title 8  2017-05-17 3.0 
Title 9  2016-05-18 2.9 
Title 10 2016-05-18 4.0 
Title 11 2016-05-19 6.1 
Title 12 2016-05-19 4.7 
Title 13 2016-05-19 8.0 
Title 14 2016-05-19 3.3 
+0

データ「APRA」の再現可能な例を入力してください。 – www

+0

ggplotの主な呼び出しで、 'aes(Post_Day、\' Visit_Time_Per_Page_(Minutes)\ ')'に変更してください。次に、 'geom_line'の代わりに' stat_summary(fun.y = mean、geom = "line") 'を実行します。 – eipi10

+0

@ycwサンプルデータを追加しました。 @ eipi10これはうまくいっていますが、このグラフに 'geom_smooth()'を追加する必要があります。 – jceg316

答えて

0

一例としてのプロットを生成することが容易であるため、20162017からすべてのレコードを変更することにより、入力データを変更しました。

キーはstat_summary関数を使用し、関数とgeomを指定することです。

# Load packages 
library(dplyr) 
library(ggplot2) 
library(lubridate) 

# Read the data 
APRA <- read.table(text = "Post_Title Post_Day 'Visit_Time_Per_Page_(Minutes)' 
'Title 1' '2016-05-15' 4.7 
'Title 2'  '2016-05-15' 3.8 
'Title 3'  '2016-05-15' 5.3 
'Title 4'  '2016-05-16' 2.9 
'Title 5'  '2016-05-17' 5.0 
'Title 6' '2016-05-17' 4.3 
'Title 7'  '2016-05-17' 4.7 
'Title 8'  '2016-05-17' 3.0 
'Title 9'  '2016-05-18' 2.9 
'Title 10' '2016-05-18' 4.0 
'Title 11' '2016-05-19' 6.1 
'Title 12' '2016-05-19' 4.7 
'Title 13' '2016-05-19' 8.0 
'Title 14' '2016-05-19' 3.3", 
       header = TRUE, stringsAsFactors = FALSE) 

# Process and plot the data 
APRA %>% 
    mutate(Post_Day = ymd(Post_Day)) %>% 
    ggplot(aes(x = Post_Day, y = Visit_Time_Per_Page_.Minutes.)) + 
    geom_point() + 
    # Calculate the mean based on y, set geom = line 
    stat_summary(fun.y = "mean", colour = "red", size = 2, geom = "line") 
関連する問題