Plot timelines without date

Viewed 32

I have a carbon dioxide sensor that captures the concentration during working time. Now I'd like to create a line plot that shows one line for each working day over time (from 8 am to 6 pm).

Some sample data:

co2data <- data.frame(
  dateTime = c(
    "2021-08-18 08:00:00",
    "2021-08-18 09:00:00",
    "2021-08-18 10:00:00",
    "2021-08-18 11:00:00",
    "2021-08-18 12:00:00",
    "2021-08-18 13:00:00",
    "2021-08-18 14:00:00",
    "2021-08-18 15:00:00",
    "2021-08-18 16:00:00",
    "2021-08-18 17:00:00",
    "2021-08-18 18:00:00",
    "2021-08-19 08:00:00",
    "2021-08-19 09:00:00",
    "2021-08-19 10:00:00",
    "2021-08-19 11:00:00",
    "2021-08-19 12:00:00",
    "2021-08-19 13:00:00",
    "2021-08-19 14:00:00",
    "2021-08-19 15:00:00",
    "2021-08-19 16:00:00",
    "2021-08-19 17:00:00",
    "2021-08-19 18:00:00"
    
  ),
  ppm = c(
    400,
    450,
    456,
    560,
    670,
    690,
    810,
    900,
    600,
    650,
    700,
  
    410,
    470,
    480,
    590,
    700,
    710,
    810,
    900,
    1010,
    1000,
    1100 
    
  )
)

Now I can plot the concentration over time, but I don't know how to plot times only on the x-axis (no dates).

co2data <- co2data %>% mutate(dateTime = as.POSIXct(dateTime))

co2data %>%
  ggplot(aes(x = dateTime, y = ppm)) + 
  geom_line() +
  labs(title = "CO2-Concentration", y = "CO2-concentration ppm", x = "Time")

How can I plot one line for each day?

1 Answers

With the help of the data.table package (or lubridate) you can extract temporal information form Date/Time strings.

require(data.table)
require(ggplot2)

setDT(co2data)

co2data[ , time := hour(as.ITime(dateTime)) ]
co2data[ , yday := as.factor(yday(as.IDate(dateTime))) ]

ggplot(co2data, aes(x = time, y = ppm, col = yday)) +
  geom_line() +
  labs(title = "CO2-Concentration", y = "CO2-concentration ppm", x = "Time") +
  theme_bw()
Related