jceg316
jceg316

Reputation: 489

how to plot the mean over time in ggplot2 R

I'm looking to plot the mean of time on site over time. My dataset is called APRA, it has a column called Post_Day which contains the date as POSIXct and a column called Visit_Time_Per_Page_(Minutes) which is a num format.

When I type this:

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)")

I get this back:

enter image description here

What I'm after is the daily average plotted over time.

Thanks.

Sample of data:

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

Upvotes: 1

Views: 5685

Answers (1)

www
www

Reputation: 39154

I changed the input data by changing all records from 2017 to 2016 because it is easier to generate plot as an example.

The key is to use stat_summary function and specify the function and 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")

Upvotes: 1

Related Questions