Search code examples
rfunctionopenair

Openair: calendarPlot not reading dataframe


I am trying to use the calendarPlot function in the openair package in R. If I load the mydata dataframe that comes with the package and run the following code it works fine:

calendarPlot(mydata, pollutant = "co", year = 2003)

mydata dataframe looks like this:

'data.frame':   65533 obs. of  10 variables:
 $ date: POSIXt, format: "1998-01-01 00:00:00" "1998-01-01 01:00:00" "1998-    01-01 02:00:00" "1998-01-01 03:00:00" ...
 $ ws  : num  0.6 2.16 2.76 2.16 2.4 3 3 3 3.36 3.96 ...
 $ wd  : int  280 230 190 170 180 190 140 170 170 170 ...
 $ nox : int  285 NA NA 493 468 264 171 195 137 113 ...
 $ no2 : int  39 NA NA 52 78 42 38 51 42 39 ...
 $ o3  : int  1 NA 3 3 2 0 0 0 1 2 ...
 $ pm10: int  29 37 34 35 34 16 11 12 12 12 ...
 $ so2 : num  4.72 NA 6.83 7.66 8.07 ...
 $ co  : num  3.37 NA 9.6 10.22 8.91 ...
 $ pm25: int  NA NA NA NA NA NA NA NA NA NA ...

I have my own dataframe, R1_Temp_cut, that looks like this:

'data.frame':   722880 obs. of  3 variables:
 $ DateTime: POSIXct, format: "2013-10-01 00:01:00" "2013-10-01 00:02:00"     "2013-10-01 00:03:00" "2013-10-01 00:04:00" ...
 $ Air.Temp: num  17.5 17.4 17.4 17.7 17.7 17.7 17.7 17.6 17.6 17 ...
 $ AirTemp : num  17.5 17.4 17.4 17.7 17.7 17.7 17.7 17.6 17.6 17 ...

When I run the the same code on my data I get the following errors:

> calendarPlot(R1_Temp_cut, pollutant = "AirTemp", year = 2014)
Error in calendarPlot(R1_Temp_cut, pollutant = "AirTemp", year = 2014) : 
  No data to plot - check year chosen
In addition: Warning message:
In match(x, table, nomatch = 0L) : NAs introduced by coercion

I noticed that the date format in mydata was POSIXt and R1_Temp_cut was POSIXct. I changed the format to POSIXct and it still worked fine. The help file says that the date format should be POSIXct.

Summary of dataframe R1_Temp_cut:

> summary(R1_Temp_cut)
    DateTime                      Air.Temp        AirTemp     
 Min.   :2013-10-01 00:01:00   Min.   :-4.10   Min.   : 0.00  
 1st Qu.:2014-02-03 11:46:45   1st Qu.:14.40   1st Qu.:14.40  
 Median :2014-06-09 05:15:30   Median :18.20   Median :18.20  
 Mean   :2014-06-09 07:11:51   Mean   :18.57   Mean   :18.57  
 3rd Qu.:2014-10-13 01:41:15   3rd Qu.:22.00   3rd Qu.:22.00  
 Max.   :2015-02-15 23:59:00   Max.   :99.00   Max.   :99.00  

Any ideas on how I can get the script to run with R1_Temp_cut dataframe would be greatly appreciated.

Thanks, Craig.


Solution

  • Turns out the function calendarPlot has to have the 'date' column labelled as date to get it to work. I changed my column name from 'DateTime' to 'date' and it worked perfectly:

    names(R1_Temp_cut)[1] <- "date" 
    calendarPlot(R1_Temp_cut, pollutant = "Air.Temp", year =2014)
    

    Output from calendarPlot