R Instructions: Boxplot

To open R with the dataset preloaded, right-click here and choose "Save Target As" to download the file to your computer. Then find the downloaded file and double-click it to open it in R.

The data have been loaded into the data frame grad_data. Enter the command grad_data to see the data. There are 6 variables (column titles) in the data frame grad_data: College.A, College.B, College.C, College.D, College.E, and College.F.

You should see graduation data for six colleges over the past eight years. Copy the next command to see a summary of the data for each college:

summary(grad_data)

By using the summary() command on the data frame instead of an individual variable, the summary statement of the five-number summary and mean are provided for each variable in the data frame.

Finally, copy the next command to see side-by-side boxplots of the graduation data for the six colleges:

boxplot(grad_data)

The above command works because all six variables are listed in separate columns. Data organization often plays a role in how you structure an R command. For example, if the data was instead organized into two columns GradRate and College in a new data frame called grad2, where GradRate contained all the numeric responses and College contained the labels A, B, C, etc., then the code would be as follows:

boxplot(grad2$GradRate~grad2$College)

Just as we did with the histogram, we can add x-axis and y-axis labels and titles using the same additional parameters in the boxplot() command, xlab=, ylab=, and main=. For example:

boxplot(grad_data, xlab="Colleges",ylab ="Graduation Rates", main="Comparison of Graduation Rates")

We can also modify the direction of the boxes by adding another parameter horizontal=TRUE. For example:

boxplot(grad_data, horizontal=TRUE, ylab="Colleges",xlab ="Graduation Rates", main="Comparison of Graduation Rates")

Notice that you must switch the x- and y-axis labels when you make a horizontal boxplot.

Note: Using R - Use the mouse to grab the corner of the graph window and change its shape. If you make the window wider, you see a label for each boxplot. While the graph window is the active window, try clicking the File menu. If you hold the mouse over "Copy to Clipboard," you see two ways that you can copy a graph for pasting into another document. This is how you use R to create data graphs for reports.

R coding Note for reference only: The above command works because all six variables are listed in separate columns. Data organization often plays a role in how you structure an R command. For example, if the data was instead organized into two columns GradRate and College in a new data frame called grad2, where GradRate contained all the numeric responses and College contained the labels A, B, C, etc., then the code would be as follows:

boxplot(grad2$GradRate~grad2$College)