aesthetics_check returns error when attempting to create a time series of sensor data

R Markdown

Hello, I am extremely new at using R and have been trying to construct a time series of my air quality sensor data for about two weeks. The ultimate goal is to have 2 sensors plotted against the reference device on the same time series.

The reference data comes directly from a SQL database - I have not changed the date_time column at all.

The code that I use to extract the data into the data frame I'm using is below (link for data files):

dat.GRI1 <- read.csv("C:\\Local\\GRI1_Calibration_Consolidated_Data.csv")
dat.GRI2 <- read.csv ("C:\\Local\\GRI2_Calibration_Consolidated_Data.csv")

dat <- data.frame()
dat <- cbind.data.frame(dat.GRI1$AEROSOL, dat.GRI2$AEROSOL, stringsAsFactors = FALSE)
colnames(dat) <- c("GRI1", "GRI2")

#Convert to ug/m3 for devices
sensorData <- dat * 1000
ref.db <- read.csv("C:\\Local\\06202022_07042022_PM3_1_min_SHARP.csv", stringsAsFactors = FALSE)

sensorCal <- data.frame()
sensorCal <- cbind.data.frame(sensorData, ref.db, stringsAsFactors = FALSE)
colnames(sensorCal) <- c("GRI1", "GRI2", "Date_Time", "Ref_PM2.5", "Ref_RH", "Ref_Temp")

clean.sensorCal <- data.frame()

clean.sensorCal <- sensorCal
clean.sensorCal[clean.sensorCal < 0] <- NA

sensorCalib <- na.omit(clean.sensorCal)
colnames(sensorCalib) <- c("GRI1", "GRI2", "Date_Time", "Ref_PM2.5", "Ref_PM2.5_ppm", "Ref_RH", "Ref_Temp")
head(sensorCalib)

The code that I'm using to try and plot a time series is below:

library(tidyverse)
library(lubridate)
library(ggplot2)

sensorCalib %>%
ggplot(aes(x = as_datetime(ymd_hm(sensorCalib$Date_Time)), y = sensorCalib$GRI1,
           type = "l",
           col = 2,
           xlim = as_datetime(c(ymd_hm("2022-06-20 10:30"), ymd_hm("2022-07-04 10:31")), tz = "GMT"),
                              xlab = "Date",
                              ylab = "PM2.5 Concentration (ug/m3)",
                              main = "DustTrak II vs SHARP Jun 20 - Jul 4, 2022"))

Included a SS of my console

You are mixing base R with ggplot2 syntax, that is not possible. I think this is what you are trying to accomplish.

library(tidyverse)
library(lubridate)

sensorCalib %>%
    mutate(Date_Time = ymd_hm(Date_Time)) %>% 
    pivot_longer(cols = c(GRI1, GRI2, Ref_PM2.5),
                 names_to = "Origin",
                 values_to = "Concentration") %>% 
    ggplot(aes(x = Date_Time, y = Concentration, color = Origin)) +
    geom_line() +
    labs(title = "DustTrak II vs SHARP Jun 20 - Jul 4, 2022",
         x = "Date",
         y = "PM2.5 Concentration (ug/m3)")

2 Likes

I tried your solution and I got the following error message:

"Error in seq.int(0, to0 - from, by) : 'to' must be a finite number
In addition: Warning message:
Problem while computing Date_Time = ymd_hm(Date_Time).
:information_source: All formats failed to parse. No formats found."

It works for me with the specific sample data and code you have provided. Can you please provide a proper REPRoducible EXample (reprex) illustrating this issue?

I'm not sure - I did the exact same thing as before except I'm at work instead of at home so the file path is slightly different. Everything else is the same.

dat.GRI1 <- read.csv("C:\\Users\\krist\\Desktop\\Calibration\\GRI1_Calibration_Consolidated_Data.csv")

dat.GRI2 <- read.csv("C:\\Users\\krist\\Desktop\\Calibration\\GRI2_Calibration_Consolidated_Data.csv")


dat <- data.frame()
dat <- cbind.data.frame(dat.GRI1$AEROSOL, dat.GRI2$AEROSOL, stringsAsFactors = FALSE)
colnames(dat) <- c("GRI1", "GRI2")

#Convert to ug/m3 esp. for DustTraks
sensorData <- dat * 1000

ref.db <- read.csv("C:\\Users\\krist\\Desktop\\Calibration\\06202022_07042022_PM3_1_min_SHARP.csv", stringsAsFactors = FALSE)


sensorCal <- data.frame()
sensorCal <- cbind.data.frame(sensorData, ref.db, stringsAsFactors = FALSE)
colnames(sensorCal) <- c("GRI1", "GRI2", "Date_Time", "Ref_PM2.5", "Ref_RH", "Ref_Temp")

clean.sensorCal <- data.frame()

clean.sensorCal <- sensorCal
clean.sensorCal[clean.sensorCal < 0] <- NA

sensorCalib <- na.omit(clean.sensorCal)
colnames(sensorCalib) <- c("GRI1", "GRI2", "Date_Time", "Ref_PM2.5", "Ref_RH", "Ref_Temp")
head(sensorCalib)

This suggests the data you are actually using is different than the sample data you have provided. Sadly, I can't help you with your issue if I can't reproduce the problem. I strongly encourage you to read the reprex guide in the link I gave you and try to provide a proper reproducible example. (one that actually shows your problem).

This topic was automatically closed 7 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.