Learn R Programming

openair (version 0.3-13)

scatterPlot: Flexible scatterPlots

Description

Scatter plots with conditioning and three main approaches: conventional scatterPlot, hexagonal binning and kernel density estimates. The former also has options for fitting smooth fits and linear models with uncertainties shown.

Usage

scatterPlot(mydata,
x = "nox",
y = "no2",
method = "scatter",
group = FALSE,
avg.time = "default",
data.thresh = 0,
statistic = "mean",
percentile = 95,
type = "default",
layout = c(1, 1),
smooth = TRUE,
linear = FALSE,
ci = TRUE,
cols = "hue",
main = "",
ylab = y,
xlab = x,
pch = 1,
lwd = 1,
key = TRUE,
key.title = type,
key.columns = 1,
strip = TRUE,
log.x = FALSE,
log.y = FALSE,
nbin = 256,
continuous = FALSE,
auto.text = TRUE,
...)

Arguments

mydata
A data frame containing at least two numeric variables to plot.
x
Name of the x-variable to plot.
y
Name of the y-variable to plot.
method
Methods include "scatter" (conventional scatter plot), "hexbin" (hexagonal binning using the hexbin package) and "density" (2D kernel density estimates).
group
If more than one pollutant is chosen, should they all be plotted on the same graph together? The default is FALSE, which means they are plotted in separate panels with their own scaled. If TRUE then they are plotte
avg.time
This defines the time period to average to. Can be "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year". For much increased flexibility a number can precede these options followed by a space. For example, a timeAverage of 2
data.thresh
The data capture threshold to use (%) when aggregating the data using avg.time. A value of zero means that all available data will be used in a particular period regardless if of the number of values available. Conversely, a value of
statistic
The statistic to apply when aggregating the data; default is the mean. Can be one of "mean", "max", "min", "median", "frequency", "sd", "percentile". Note that "sd" is the standard deviation and "frequency" is the number (frequency) of valid r
percentile
The percentile level in % used when statistic = "percentile" and when aggregating the data with avg.time. The default is 95. Not used if avg.time = "default".
type
The type of analysis to be done. The default is will produce a single plot using the entire data. Other types include "hour" (for hour of the day), "weekday" (for day of the week) and "month" (for month of the year), "year" for a polarPlot
layout
Determines how the panels are laid out. By default, plots will be shown in one column with the number of rows equal to the number of pollutants, for example. If the user requires 2 columns and two rows, layout should be set to layout
smooth
A smooth line is fitted to the data if TRUE; optionally with 95% confidence intervals shown.
linear
A linear model is fitted to the data if TRUE; optionally with 95% confidence intervals shown. The equation of the line and R2 value is also shown.
ci
Should the confidence intervals for the smooth/linear fit be shown?
cols
Colours to be used for plotting. Options include "default", "increment", "heat", "spectral", "hue", "brewer1" and user defined (see manual for more details). The same line colour can be set for all pollutant e.g. cols = "black"
main
The plot title; default is no title.
ylab
Name of y-axis variable. By default will use the name of y.
xlab
Name of x-axis variable. By default will use the name of x.
pch
The symbol type used for plotting. Default is to provide different symbol types for different pollutant. If one requires a single symbol for all pollutants, the set pch = 1, for example.
lwd
Not used yet.
key
Should a key be drawn? The default is TRUE.
key.title
The title of the key (if used).
key.columns
Number of columns to be used in the key. With many pollutants a single column can make to key too wide. The user can thus choose to use several columns by setting columns to be less than the number of pollutants.
strip
Should a strip be drawn? The default is TRUE.
log.x
Should the x-axis appear on a log scale? The default is FALSE. If TRUE a well-formatted log10 scale is used. This can be useful for checking linearity once logged.
log.y
Should the y-axis appear on a log scale? The default is FALSE. If TRUE a well-formatted log10 scale is used. This can be useful for checking linearity once logged.
nbin
Number of bins used for kernel density output using method "density".
continuous
When this option is TRUE a plot of x vs. y will be made, colour-coded by levels of type. A continuous separate colour scale is shown. If continuous = FALSE and type is numeric then the plot of x vs
auto.text
Either TRUE (default) or FALSE. If TRUE titles and axis labels will automatically try and format pollutant names and units properly e.g. by subscripting the `2' in NO2.
...
Other graphical parameters.

Details

The scatterPlot is the basic function for plotting scatterPlots in flexible ways in openair. It is flexible enough to consider lots of conditioning variables and takes care of fitting smooth or linearRelationships to the data. There are three main ways of plotting the relationship between two variables, which are set using the method option. The default "scatter" will plot a conventional scatterPlot. In cases where there are lots of data and over-plotting becomes a problem, then method = "hexbin" or method = "density" can be useful. The former requires the hexbin package to be installed. By default a smooth fit is shown as this can help show the overall form of the data e.g. whether the relationship appears to be linear or not. Also, a linear fit can be shown using linear = TRUE as an option. The user has fine control over the choice of colours and symbol type used. Another way of reducing the number of points used in the plots which can sometimes be useful is to aggregate the data. For example, hourly data can be aggregated to daily data. See timePlot for examples here. By default plots are shown with a colour key at the bottom and in the case of conditioning, strips on the top of each plot. Sometimes this may be overkill and the user can opt to remove the key and/or the strip by setting key and/or strip to FALSE. One reason to do this is to maximise the plotting area and therefore the information shown.

See Also

linearRelation, timePlot and timeAverage for details on selecting averaging times and other statistics in a flexible way

Examples

Run this code
# load openair data if not loaded already
data(mydata)

# basic use, single pollutant
scatterPlot(mydata, x = "nox", y = "no2")

# scatterPlot by year
scatterPlot(mydata, x = "nox", y = "no2", type = "year")

# scatterPlot by day of the week, removing key at bottom
scatterPlot(mydata, x = "nox", y = "no2", type = "weekday", key =
FALSE)

# example of the use of continuous where colour is used to show
# different levels of a third (numeric) variable
# plot daily averages and choose a filled plot symbol (pch = 16)
# select only 2004
dat2004 <- selectByDate(mydata, year = 2004)
scatterPlot(dat2004, x = "nox", y = "no2", type = "co", continuous =
 TRUE, avg.time = "day", pch = 16)

# show linear fit, by year
scatterPlot(mydata, x = "nox", y = "no2", type = "year", smooth =
FALSE, linear = TRUE)

# do the same, but for daily means...
scatterPlot(mydata, x = "nox", y = "no2", type = "year", smooth =
FALSE, linear = TRUE, avg.time = "day")

# log scales
scatterPlot(mydata, x = "nox", y = "no2", type = "year", smooth =
FALSE, linear = TRUE, avg.time = "day", log.x = TRUE, log.y = TRUE)

# also works with the x-axis in date format (alternative to timePlot)
scatterPlot(mydata, x = "date", y = "no2", avg.time = "month", key = FALSE)

# use hexagonal binning
library(hexbin)
# basic use, single pollutant
scatterPlot(mydata, x = "nox", y = "no2", method = "hexbin")

# scatterPlot by year
scatterPlot(mydata, x = "nox", y = "no2", type = "year", method = "hexbin")

Run the code above in your browser using DataLab