Bar Plots
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 |
Adelie | Dream | 37.2 | 18.1 | 178 | 3900 | male | 2007 |
Adelie | Dream | 32.1 | 15.5 | 188 | 3050 | female | 2009 |
Adelie | Dream | 39.5 | 16.7 | 178 | 3250 | female | 2007 |
Gentoo | Biscoe | 48.7 | 14.1 | 210 | 4450 | female | 2007 |
Gentoo | Biscoe | 47.5 | 14.0 | 212 | 4875 | female | 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 penuins
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 histogram 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!