Plotting times with hours and minutes

In my data, times were recorded in hours:minutes like 0:00, 0:15.
It looked like this:

time  avg_holds month
   <fct>     <dbl> <chr>
 1 0:00      1.15  June 
 2 0:15      0.396 June 
 3 0:30      0     June 
 4 0:45      4.76  June 
 5 1:00      1.59  June 
 6 1:15      0     June 
 7 1:30      0     June 
 8 1:45      0     June 
 9 10:00     2.19  June 
10 10:15     3.65  June 

I am trying to get a plot with times on the x axis, avg_holds on the y axis. The times are confusing me.

May be something like this:

dataset <- read.table(text = 'time  avg_holds month
0:00      1.15  June 
0:15      0.396 June 
0:30      0     June 
0:45      4.76  June 
1:00      1.59  June 
1:15      0     June 
1:30      0     June 
1:45      0     June 
10:00     2.19  June 
10:15     3.65  June',
                     header = TRUE,
                     stringsAsFactors = FALSE)

with(data = dataset,
     expr =
       {
         time = as.POSIXct(x = time,
                           format = "%H:%M") # assuming 24 hour time format
         plot(x = time,
              y = avg_holds)
       })

Created on 2019-08-19 by the reprex package (v0.3.0)

3 Likes

This would be a ggplot2 based option

library(tidyverse)

dataset <- data.frame(stringsAsFactors=FALSE,
        time = c("0:00", "0:15", "0:30", "0:45", "1:00", "1:15", "1:30",
                 "1:45", "10:00", "10:15"),
   avg_holds = c(1.15, 0.396, 0, 4.76, 1.59, 0, 0, 0, 2.19, 3.65),
       month = c("June", "June", "June", "June", "June", "June", "June",
                 "June", "June", "June")
)

dataset %>% 
    mutate(time = as.POSIXct(hms::parse_hm(time))) %>% 
    ggplot(aes(time, avg_holds)) +
    geom_point() +
    scale_x_datetime(date_labels = "%H:%M")

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.