# simulate data and get at least one outlier
y <- rnorm(100,50,10)
y[1] <- 90
# ------------------------------
# box plot 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=TRUE)
# vertical boxplot with plum color
BoxPlot(y, horiz=FALSE, fill="plum")
# box plot with outliers more strongly highlighted
BoxPlot(y, stroke="red", xlab="My Variable")
# ------------------------------------------------
# box plots for data frames and multiple variables
# ------------------------------------------------
# read internal lessR dataset
# mydata contains both numeric and non-numeric data
mydata <- rd("Employee", format="lessR", quiet=TRUE)
# box plot with superimposed dot plot (stripchart)
BoxPlot(Salary, add.points=TRUE)
# box plot with results saved to object b instead of displaying
b <- BoxPlot(Salary)
# show the results
b
# show just the piece regarding the statistics
b$out_stats
# list the names of all the components
names(b)
# box plot with rotated axis values, offset more from axis
BoxPlot(Salary, rotate.values=45, offset=1)
# BoxPlot generates R markdown file to be "knit"
# such as in RStudio
bx(Salary, Rmd="myout")
# box plots for all numeric variables in data frame called mydata
BoxPlot()
# box plots for all numeric variables in data frame called mydata
# with specified options
BoxPlot(fill="palegreen1", stroke="plum")
# Use the subset function to specify a variable list
# box plots for all specified numeric variables
BoxPlot(c(Salary,Years))
Run the code above in your browser using DataLab