Plotting a line graph showing date by time in R

Viewed 217

I have data that looks as follows:

Date           Time_finished
4/3/2020       16:30:21
4/6/2020       16:43:29
4/7/2020       16:28:47
4/8/2020       16:30:38
4/9/2020       16:50:01

I would like to plot a line chart showing date across the x axis and then the time finished on the y axis, to show a time series graph. For some reason this does not seem to be working, the Date is saved as Date but time as a factor, does this also need to be a date?

I have tried normal plot but having no luck.

Thanks

1 Answers

Like this?

df <- tibble::tribble(
       ~Date, ~Time_finished,
  "4/3/2020",     "16:30:21",
  "4/6/2020",     "16:43:29",
  "4/7/2020",     "16:28:47",
  "4/8/2020",     "16:30:38",
  "4/9/2020",     "16:50:01"
  )

library(tidyverse)

df %>% 
  mutate(Date = as.POSIXct(Date, format = "%m/%d/%y"),
         Time_finished = as.POSIXct(Time_finished, format = "%H:%M:%S")) %>% 
  ggplot(aes(x = Date, y = Time_finished, group = 1)) +
  geom_line() + scale_y_datetime(breaks = date_breaks("10 min"),
                                 minor_breaks = date_breaks("2 min"),
                                 labels = date_format("%Hh %Mm %Ss"))

enter image description here

Related