The openair package is specifically designed to plot air pollution data. This tutorial will give a brief introduction to many of the plotting functions in openair.
This tutorial will cover the following openair functions.
The first plotting function we’ll look at is the
summaryPlot(). Functions in the openair package expect data frames that are in a certain format. The column with the date and time information must be labeled “date” (lowercase) and be a POSIXct class.
The first data frame we’ll use is the chicago_air data frame from the region5air package, which already has a column labeled “date.”
However, the class of that “date” column is character.
We need to change it using the
as.POSIXct() function. This function will assume that the format of the date is YYYY-MM-DD. So, we just need to supply a correct time zone.
chicago_air$date <- as.POSIXct(chicago_air$date, tz = "America/Chicago")
Now we just feed the first four columns of the data frame to the
summaryPlot() function. We use the
select() function from dplyr to select our columns and we use the short hand date “solar” to select the “date” column, the “solar” column, and all the columns in between.
windRose() function expects a data frame with columns for wind speed and wind direction labeled “ws” and “wd”, respectively. Here we load the chicago_wind dataset from the region5air package and take a look at the columns.
We need to create a “date” column with a POSIXct class, so we’ll use the
as.POSIXct() function again. This time we will need to provide information in the format parameter.
chicago_wind$datetime <- as.POSIXct(chicago_wind$datetime, format ="%Y%m%dT%H%M",
tz = "America/Chicago")
Now we’ll rename the columns using the
rename() function from dplyr.
chicago_wind <- rename(chicago_wind, date = datetime, ws = wind_speed, wd = wind_dir)
Now back to our
chicago_wind data frame. We have prepared the dates and column names above so that now we can feed the data frame to the
windRose(chicago_wind, key.footer = "knots") # default is m/s
We can split the data frame by time periods by using the type argument.
windRose(chicago_wind, type = “weekday”, key.footer = “knots”)
We can make a similar plot that will display pollutant concentrations in relation to wind direction.
pollutionRose(chicago_wind, pollutant = "ozone", # we can use the breaks parameter
breaks = c(0, .02, .04, .06, .07, .08)) # to create our own breakpoints
We can also look at the values by time periods.
pollutionRose(chicago_wind, pollutant = "ozone", type = "month")
percentileRose() function calculates percentile levels of a pollutant and plots them by wind direction. This can help you quickly visually identify potential sources by wind direction.
percentileRose(chicago_wind, pollutant = "ozone", smooth =TRUE)
Time series plots can be easily produced using
timePlot(chicago_air, pollutant = c(“ozone”, “temp”, “solar”))
calendarPlot() displays daily values in a calendar format.
calendarPlot(chicago_air, pollutant = "ozone")
calendarPlot(chicago_wind, pollutant = “ozone”, annotate = “ws”)
Now, let’s move on to the first set of real exercises on the openair package!