Comparing Boxplots in R

Start by creating a new Project in RStudio and save the project in your lectures folder with the name Boxplots2. Next, copy the file data/chapter4/dataset1.dat form the Aliaga Data Set (available at http://msemac.redwoods.edu/~darnold/math15/data.zip) into the lectures/Boxplots2 folder. Next, copy the file data/chapter4/exer4_29.dat from the Aliaga Data Set into the lectures/Boxplots2 folder. Go back to RStudio and click the Files tab and make sure that the files dataset1.dat and exer4_29.dat both appear in your files folder.

Next, create a new R script file and save it with the name Boxplots2. It should now appear in your RStudio files folder with the name Boxplots2.R.

Entering Your Own Data

R’s boxplot command has several levels of use, some quite easy, some a bit more difficult to learn. Let’s start with an easy example. You can enter your own data manually and then create a boxplot.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
boxplot(x)

If you’d like to compare two sets of data, enter each set separately, then enter them individually into the boxplot command.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
y=c(5,6,7,7,8,10,1,1,15,23,44,76)
boxplot(x,y)

You can easily compare three sets of data. Just enter your three sets of data and then enter them individually into the boxplot command.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
y=c(5,6,7,7,8,10,1,1,15,23,44,76)
z=c(15,15,15,16,19,25,29,30,55,79)
boxplot(x,y,z)

You can use the argument horizontal=TRUE to lay them out horizontally.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
y=c(5,6,7,7,8,10,1,1,15,23,44,76)
z=c(15,15,15,16,19,25,29,30,55,79)
boxplot(x,y,z,
        horizontal=TRUE)

You can add names to each boxplot.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
y=c(5,6,7,7,8,10,1,1,15,23,44,76)
z=c(15,15,15,16,19,25,29,30,55,79)
boxplot(x,y,z,
        horizontal=TRUE,
        names=c("Level 1","Level 2","Level 3"))

You can add different colors.

x=c(1,2,3,3,4,5,5,7,9,9,15,25)
y=c(5,6,7,7,8,10,1,1,15,23,44,76)
z=c(15,15,15,16,19,25,29,30,55,79)
boxplot(x,y,z,
        horizontal=TRUE,
        names=c("Level 1","Level 2","Level 3"),
        col=c("red","yellow","blue"))