bx
Uses the standard R boxplot function, boxplot
to display a boxplot in color. Also display the relevant statistics such as the hinges, median and IQR.
If the provided object for which to calculate the box plot is a data frame, then a box plot is calculated for each numeric variable in the data frame and the results written to a pdf file in the current working directory. The name of this file and its path are specified in the output.
BoxPlot(x=NULL, dframe=mydata, n.cat=getOption("n.cat"), text.out=TRUE, ...)## S3 method for class 'data.frame':
bx(x, n.cat, text.out, \ldots)
## S3 method for class 'default':
bx(x,
col.fill=getOption("col.fill.pt"),
col.stroke=getOption("col.stroke.pt"),
col.bg=getOption("col.bg"), col.grid=getOption("col.grid"),
cex.axis=.85, col.axis="gray30", col.ticks="gray30",
xlab=NULL, main=NULL, digits.d=NULL, text.out=TRUE,
horiz=TRUE, add.points=FALSE,
pdf.file=NULL, pdf.width=5, pdf.height=5, ...)
bx(...)
dframe
, that is, no variable specified, then the
data frame mydata
is assumed.mydata
.cex.axis.
TRUE
, then display text output in console.FALSE
for vertical.TRUE
, then place a dot plot (i.e., stripchart) over the
box plot.boxplot
, the default here is for a horizontal boxplot. Also, BoxPlot
does not currently process in formula mode, so use the standard R boxplot
function to process a formula in which a boxplot is displayed for a variable at each level of a second, usually categorical, variable.Other graphic parameters are available to format the display, such as main
for the title, and other parameters found in boxplot
and par
. To minimize white space around the boxplot, re-size the graphics window before or after creating the boxplot.
den
.
DATA
If the variable is in a data frame, the input data frame has the assumed name of mydata
. If this data frame is named something different, then specify the name with the dframe
option. Regardless of its name, the data frame need not be attached to reference the variable directly by its name, that is, no need to invoke the mydata$name
notation.
To obtain a box plot of each numerical variable in the mydata
data frame, use BoxPlot()
. Or, for a data frame with a different name, insert the name between the parentheses.
COLORS
Individual colors in the plot can be manipulated with options such as col.bars
for the color of the histogram bars. A color theme for all the colors can be chosen for a specific plot with the colors
option with the lessR
function set
. The default color theme is blue
, but a gray scale is available with "gray"
, and other themes are available as explained in set
, such as "red"
and "green"
. Use the option ghost=TRUE
for a black backgound, no gridlines and partial transaparency of plotted colors.
VARIABLE LABELS
Although standard R does not provide for variable labels, lessR
can store the labels in a data frame called mylabels
, obtained from the Read
function. If this labels data frame exists, then the corresponding variable label is by default listed as the label for the horizontal axis and on the text output. For more information, see Read
.
PDF OUTPUT
Because of the customized graphic windowing system that maintains a unique graphic window for the Help function, the standard graphic output functions such as pdf
do not work with the lessR
graphics functions. Instead, to obtain pdf output, use the pdf.file
option, perhaps with the optional pdf.width
and pdf.height
options. These files are written to the default working directory, which can be explicitly specified with the R setwd
function.
ONLY VARIABLES ARE REFERENCED
The referenced variable in a lessR
function can only be a variable name. This referenced variable must exist in either the referenced data frame, mydata
by default, or in the user's workspace, more formally called the global environment. That is, expressions cannot be directly evaluated. For example:
> BoxPlot(rnorm(50)) # does NOT work}
Instead, do the following: > Y <- rnorm(50) # create vector Y in user workspace > BoxPlot(Y) # directly reference Y
[object Object],[object Object]
# ----------------------------- # boxplot for a single variable # -----------------------------
# standard horizontal boxplot with all defaults BoxPlot(y)
# short name bx(y)
# save the box plot to a pdf file BoxPlot(y, pdf.file="MyBoxPlot.pdf")
# vertical boxplot with plum color BoxPlot(y, horiz=FALSE, col.fill="plum")
# boxplot with outliers more strongly highlighted BoxPlot(y, col.stroke="red", xlab="My Variable")
# ----------------------------------------------- # boxplots for data frames and multiple variables # -----------------------------------------------
# create data frame, mydata, to mimic reading data with rad function # mydata contains both numeric and non-numeric data mydata <- data.frame(rnorm(100), rnorm(100), rnorm(100), rep(c("A","B"),50)) names(mydata) <- c("X","Y","Z","C")
# boxplot for variable X from data frame, referred to directly BoxPlot(X)
# boxplot with superimposed dot plot (stripchart) BoxPlot(X, dotplot=TRUE)
# boxplots for all numeric variables in data frame called mydata BoxPlot()
# boxplots for all numeric variables in data frame called mydata # with specified options BoxPlot(col.fill="palegreen1", col.stroke="plum")
# Use the subset function to specify a variable list
mysub <- subset(mydata, select=c(X,Y))
BoxPlot(dframe=mysub)