Plot Vertical Tukey Boxplots
Plots a series of vertical Tukey boxplots where the individual boxplots represent the data subdivided by the value of some factor. Optionally the y-axis may be scaled logarithmically (base 10) and the values of the Tukey fences used to identify near and far outliers may also be optionally based on the logarithmically transformed data. A variety of other plot options are available, see Details and Note below.
tbplots(x, by, log = FALSE, logx = FALSE, notch = TRUE, xlab = "", ylab = deparse(substitute(x)), ylim = NULL, main = "", label = NULL, plot.order = NULL, xpos = NA, width, space = 0.25, las = 1, cex = 1, adj = 0.5, add = FALSE, ssll = 1, colr = 8, ...)
x |
name of the variable to be plotted. |
by |
the name of the factor variable to be used to subdivide the data. See Details below for when |
log |
to display the data with logarithmic (y-axis) scaling, set |
logx |
if the position of the Tukey boxplot fences are to be computed on the basis of the log transformed data set |
notch |
determines if the boxplots are to be “notched” such that the notches indicate the 95% confidence intervals for the medians. The default is to notch the boxplets, to suppress the notches set |
xlab |
a title for the x-axis, by default none is provided. |
ylab |
by default the character string for |
ylim |
only for |
main |
a main title may be added optionally above the display by setting |
label |
by default the character strings defining the factors are used to label the boxplots along the x-axis. Alternate labels can be provided with |
plot.order |
provides an alternate order for the boxplots. Thus, |
xpos |
the locations along the x-axis for the individual vertical boxplots to be plotted. By default this is set to |
width |
the width of the boxes, by default this is set to the minimum distance between all adjacent boxplots times the value of |
space |
the space between the individual boxplots, by default this is 0.25 x-axis units. |
las |
controls whether the x-axis labels are written parallel to the x-axis, the default |
cex |
controls the size of the font used for the factor labels plotted along the x-axis. By default this is |
adj |
controls the justification of the x-axis labels. By default they are centred, |
add |
permits the user to plot additional boxplots into an existing display. It is recommended that this option is left as |
ssll |
determines the minimum data subset size for which a subset will be plotted. By default this is set to |
colr |
by default the boxes are infilled in grey, |
... |
further arguments to be passed to methods. For example, the size of the axis titles by setting |
There are two ways to execute this function. Firstly by defining x
and by
, and secondly by combining the two variables with the split
function. See the first two examples below. The split
function can be useful if the factors to use in the boxplot are to be generated at run-time, see the last example below. Note that when the split
construct is used instead of by
the whole split
statement will be displayed as the default y-axis title. Also note that when using by
the subsets are listed in the order that the factors are encountered in the data, but when using split
the subsets are listed alphabetically. In either case they can be re-ordered using plot.order
, see Examples.
The width
option can be used to define different widths for the individual boxplots. For example, the widths could be scaled to be proportional to the subset population sizes as some function of the square root (const * sqrt(n)) or logarithm (const * log10(n)) of those sizes (n). The constant, const, would need to be chosen so that on average the width of the individual boxes would be approximately 0.25, see Example below. It may be desirable for cosmetic purposes to adjust the positions of the boxes along the x-axis, this can be achieved by specifying xpos
.
Long subset (factor) names can lead to display problems, changing the las
parameter from its default of las = 1
which plots subset labels parallel to the axis to las = 2
, to plot perpendicular to the axis, can help. It may also help to use label
and split the character string into two lines, e.g., by changing the string "Granodiorite"
that was supplied to replace the coded factor variable GRDR
to "Grano-\ndiorite"
. If this, or setting las = 2
, causes a conflict with the x-axis title, if one is needed, the title can be moved down a line by using xlab = "\nLithological Units"
. In both cases the \n
forces the following text to be placed on the next lower line.
If there are more than 7 labels (subsets) and no alternate labels are provided las is set to 2, otherwise some labels may fail to be displayed.
The notches in the boxplots indicate the 95% confidence intervals for the medians and can extend beyond the upper and lower limits of the boxes indicating the middle 50% of the data when subset population sizes are small. The confidence intervals are estimated using the binomial theorem. It can be argued that for small populations a normal approximation would be better. However, it was decided to remain with a non-parametric estimate despite the fact that the calculation of the Tukey fence values involves normality assumptions.
This function is based on a script shared by Doug Nychka on S-News, April 28, 1992.
Any less than detection limit values represented by negative values, or zeros or other numeric codes representing blanks in the data, must be removed prior to executing this function, see ltdl.fix.df
.
Any NA
s in the data vector are removed prior to preparing the boxplots.
For summary statistics displays to complement the graphics see gx.summary.groups
or framework.summary
.
Douglas W. Nychka and Robert G. Garrett
## Make test data kola.c available data(kola.c) attach(kola.c) ## Display a simple Tukey boxplot tbplots(Cu, by = COUNTRY) tbplots(split(Cu,COUNTRY)) ## Display a more appropriately labelled and scaled boxplot tbplots(Cu, by = COUNTRY, log = TRUE, logx = TRUE, xlab = "Country", ylab = "Ni (mg/kg) in <2 mm Kola C-horizon soil") ## Display a west-to-east re-ordered plot using the full country names tbplots(split(Cu, COUNTRY), log = TRUE, logx = TRUE, ylab = "Ni (mg/kg) in <2 mm Kola C-horizon soil", label = c("Finland", "Norway", "Russia"), plot.order = c(2, 1, 3)) ## Detach test data kola.c detach(kola.c) ## Make test data kola.o available, setting a -9999, indicating a ## missing pH measurement, to NA data(kola.o) kola.o.fixed <- ltdl.fix.df(kola.o, coded = -9999) attach(kola.o.fixed) ## Display relationship between pH in one pH unit intervals and Cu in ## O-horizon (humus) soil tbplots(split(Cu,trunc(pH+0.5)), log=TRUE, logx = TRUE, xlab = "O-horizon soil pH to the nearest pH unit", ylab = "Cu (mg/kg) in <2 mm Kola O-horizon soil") ## As above, but demonstrating the use of variable box widths and the ## suppression of 95% confidence interval notches. The box widths are ## computed as (Log10(n)+0.1)/5, the 0.1 is added as one subset has a ## population of 1. Note: paste is used in constructing xlab, below, ## as the label is long and overflows the text line length table(trunc(pH+0.5)) tbplots(split(Cu,trunc(pH+0.5)), log=TRUE, logx = TRUE, notch = FALSE, xlab = paste("O-horizon soil pH to the nearest pH unit,", "\nbox widths proportional to Log(subset_size)"), ylab = "Cu (mg/kg) in <2 mm Kola O-horizon soil", width = c(0.26, 0.58, 0.24, 0.02)) ## Detach test data kola.o.fixed detach(kola.o.fixed)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.