Boxplots

Getting Started

First, be sure you have installed ggformula. Remember, you only need to install the package once on your machine.

Then, be sure to load the package ggformula. Remember, you need to do this with each new Quarto/RMarkdown document or R Session.

Data for Examples

As a reminder (see Overview of Data Visualization), we will be using the penguins data from the palmerpenguins package:

library(palmerpenguins)

Here is a snippet of the data:

Palmer Penguins
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Gentoo Biscoe 46.5 14.5 213 4400 female 2007
Gentoo Biscoe 55.9 17.0 228 5600 male 2009
Gentoo Biscoe 49.9 16.1 213 5400 male 2009
Chinstrap Dream 50.5 18.4 200 3400 female 2008
Chinstrap Dream 51.9 19.5 206 3950 male 2009

Boxplots with One Quantitative Variable

Basic Code

For a single quantitative variable, x, here is the general structure for a boxplot.

gf_boxplot(~x, 
           data = mydata)

Notice that a y-axis is added, but it does not have any meaning. We can remove it using the function gf_theme()

gf_boxplot(~x, 
           data = mydata) |> #this is a pipe, another option is %>%
  gf_theme(axis.ticks.y = element_blank(),  #removes y-axis ticks
           axis.text.y = element_blank())   #removes y-axis labels

Notice the use of the native pipe |> to connect the two functions from ggformula. Another version of a pipe in R is %>%. Both will work, it is just that %>% relies on another pacakge and |> is built into base R.

Run the code below to see an example using the quantitative variable bill_length_mm from the penguins data. Then replace bill_length_mm with another quantitative variable from the penguins data (e.g. bill_depth_mm)

Notice the warning produced from running the code. This is just a warning that there were rows (penguins) ignored due to missing data for the variables visualized.

Adding Labels

Descriptive labels are important for any visualization. We can always add them to any visualization by adding xlab = to your function. Notice, for a single variable we do not add a y-axis label.

gf_boxplot(~x, 
           data = mydata,
           xlab = "X Axis Label",
           title = "Descriptive Title") |> 
  gf_theme(axis.ticks.y = element_blank(), 
           axis.text.y = element_blank())

Add labels and a title to the boxplot for bill_length_mm.

Other Modifications

We can add a few other modifications that purely aesthetic - just to make our graphs look nicer or easier to read.

Adding the Data Points to the Graph

Boxplots often hide the amount of data they represent (the number of cases). To combat this, we can add the data points to the boxplot using the gf_jitter() function. You can modify the height = argument to change the distance between points. Notice the 0 which centers the points on the y-axis at 0.

gf_boxplot(~x, 
           data = mydata) |> 
  gf_jitter(0 ~ x, 
           data = mydata,
           height = 1) |> 
  gf_theme(axis.ticks.y = element_blank(),  
           axis.text.y = element_blank())   

Modify the values for height = agrument and see how they affect the point distribution on the y-axis. Remember, the y-axis has no meaning here, so the heights of the points are random and not representative of any element of the data. Just the position on the x-axis is meaningful.

Filling the Boxes with Color

We can add a color to fill the boxes by telling R to fill the bars with a specified color either using a built in color from R or using a hex code for colors .

gf_boxplot(~x, 
           data = mydata,
           xlab = "X Axis Label",
           title = "Descriptive Title",
           fill = "darkcyan") |> 
  gf_theme(axis.ticks.y = element_blank(), 
           axis.text.y = element_blank())

Changing the Theme

The package ggformula is built on top of another package called ggplot2 and so any ggplot2 function can be added to a ggformula generated graphic. For example, we can change the theme to a built-in theme. To connect ggplot2 functions, we will use a + at the end of the line precedding the function.

Try changing the theme to the following graph:

Boxplots for Comparisons Across Groups

When we have a quantitative variable that has been measured across multiple groups, we may be interested in comparing boxplots across the values/groups of a categorical variable. We can do this by adding a y-axis variable that represents the categorical variable.

gf_boxplot(y ~ x, 
           data = mydata) 
#we don't need to do anything to the y-axis now because it represents a variable here 

The variable places in the y position will be on the y-axis and the variable in the x position will be on the x-axis. So we can change the orientation of our boxplot by switching the position of the quantitative and categorical variable.

Here is an example with species on the y-axis. Try modifying the code so that species is on the x-axis and bill_length_mm is on the y-axis. Then modify the axes labels.

Adding Jitter to Groups

It is much easier to add jitter points to the boxplot across groups, we just pipe into the gf_jitter() function. Modify the height = argument to adjust the random position of the points on the y-axis.

Adding Color to Groups

Similar to changing the color of boxes to a single color, we can use the fill = argument associated with the categorical variable.

Here is the boxplot of bill_length_mm with fill color varied by species a categorical variable with values of Adelie, Chinstrap, and Gentoo. Modify the code below to change the fill color to another categorical variable such as island or sex and see what happens.

If we want to specify color and add jitter, we can do that too!