Error plotting data

Using the code below, I am able to produce an output, however, the start_time when plotted is 06:00 instead of 09:00, and the end_time is 12:00, instead of 15:01. I would appreciate knowing where I am going wrong.

Many thanks
Lara

library(lubridate)
library(tidyverse)
library(reshape2)
library(dplyr)
library(ggplot2)
library(lubridate)

Read the CSV file and assign it to a variable (e.g., box1077102745)

box1077102745 <- read.csv("C:/Users/ltookey/OneDrive - Massey University/03.0_PhD/DATA/IEQ_Skomobo/WMK/R11/box1077102745.csv")

Convert "Time" column to POSIXct

box1077102745$Time <- as.POSIXct(box1077102745$Time, format = "%Y-%m-%d %H:%M:%S", tz = "Pacific/Auckland")

Specify the week and time range

start_date <- as.POSIXct("2023-09-04 09:00:00", tz = "Pacific/Auckland")
end_date <- start_date + 5 * 24 * 60 * 60 # 5 SCHOOL days in seconds
end_date <- as.POSIXct(end_date, tz = "Pacific/Auckland")

start_time <- hms("09:00:00")
end_time <- hms("15:01:00")

Create intervals for the entire week and specific time range

week_interval <- interval(start_date, end_date)
time_range_interval <- interval(start_date + start_time, start_date + end_time)

Filter data for the specific week and time range

filtered_data <- box1077102745 %>%
filter(
ymd_hms(Time) >= start_date,
ymd_hms(Time) <= end_date,
ymd_hms(Time) >= start_date + start_time,
ymd_hms(Time) <= start_date + end_time
)

str(filtered_data)

Reshape filtered data using melt

plot_data <- melt(filtered_data, id.vars = 'Time', measure.vars = c('Temp', 'Humid', 'CO2'))

Create a multivariate time series plot for the selected week and time range

ggplot(plot_data, aes(x = Time, y = value, color = variable)) +
geom_line() +
facet_wrap(~variable, scales = "free_y", ncol = 1) +
labs(title = "Multivariate Time Series Plot of IEQ Data (Selected Week and Time Range)",
x = "Time",
y = "Value") +
scale_color_manual(values = c("Temp" = "blue", "Humid" = "green", "CO2" = "red")) +
theme_minimal()

It is tricky to fully analyse your code without seeing the data, but I noticed one thing. Your start_date is 2023-09-04 09:00:00. So, it already includes the start time of 09:00:00. If you add start_date + start_time, you will obtain 2023-09-04 18:00:00, and start_date + end_time will result in 2023-09-05 00:01:00. I don't suppose you intended to do this. Also, your filter expression includes <= start_date + end_time, but I suspect you wanted end_date. Perhaps this would help:

filtered_data <- box1077102745 %>%
  filter(
    ymd_hms(Time) >= start_date,
    ymd_hms(Time) <= end_date,
  )

You would need to change end_date to 2023-09-09 15:01:00 first.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.