# How to create bivariate plots to compare groups (in R, using lattice and gplots)

See all solutions.

Suppose we have a dataset with different treatment conditions and an outcome variable, and we want to perform exploratory data analysis. How would we visually compare the treatment conditions with regards to the outcome variable?

## Solution

We use a built-in dataset called ToothGrowth that discusses the length of the teeth (len) in each of 10 guinea pigs at three Vitamin C dosage levels ($0.5$, $1$, and $2$ mg) with two delivery methods - orange juice or ascorbic acid (supp).

1
2
# You can replace this example data frame with your own data
df <- ToothGrowth


If you wish to understand the distribution of the length of the tooth based on the delivery methods, you can construct a bivariate histogram plot.

1
2
3
# install.packages( "lattice" ) # if you have not already done this
library(lattice)
histogram( ~ len | supp, data = df)


To visualize the summary statistics of the length of the tooth based on the delivery methods, you can construct a bivariate box plot.

1
2
3
bwplot(df$len ~ df$supp)
# Or the following code produces a similar figure, using the mosaic package:
# boxplot(len ~ supp, data = df)


To plot the means for both treatment levels of supp for the len column, we load the gplots package and use the plotmeans function.

1
2
3
# install.packages( "gplots" ) # if you have not already done this
library(gplots)
plotmeans(df$len ~ df$supp)

1
2
3
4
5
6
Attaching package: ‘gplots’

The following object is masked from ‘package:stats’:

lowess