For the examples on this page, you’ll want to require the openintro package. Load the package into working memory. You must do this each time you use RStudio.

require(openintro)

Create a boxplot

The ageAtMar data set (part of the openintro package) contains the age at first marriage for a sample of 5,534 US women.

We can use the R function boxplot() to create a boxplot for this variable.

boxplot(ageAtMar$age)

Like with the barchart and histogram functions, we can modify the command to create titles.

boxplot(ageAtMar$age, # Specify the variable to graph
        main = "US Women and Marriage",
        ylab = "Age at First Marriage in Years") # Create a title for the plot

We can also change the orientation by including the argument horizontal = TRUE. This time label the x-axis instead of the y-axis.

boxplot(ageAtMar$age, # Specify the variable to graph
        horizontal = TRUE, # Change the orientation of the plot
        main = "US Women and Marriage",
        xlab = "Age at First Marriage in Years") # Create a title for the plot

Side-by-side boxplots

We often want to compare the numerical results of a quantitative variable based on the classification of a qualitative variable. Side-by-side boxplots allow us to do this easily. Using the boxplot() command, we name the quantitative variable first, then connect it to a qualitative variable using the tilde ‘~’. You must then specify the data set as an additional argument to the function. Let’s look at the cars data set, which includes information about 54 cars from 1993.

boxplot(weight~driveTrain, # Specify the variables to graph, quantitative variable first
        data = cars, # Specify the data set that contains the variables
        main = "Cars in 1993", #Create the chart title
        xlab = "Drive Train",  #Create the x-axis label
        ylab = "Weight in pounds") #Create the y-axis label