10 Overlapping bar graphs
10.1 Desription
Overlapping bar graphs display multiple variables on the same graph. Bars overlap instead of being placed side by side. Bars are often made partially transparent to aid visibility. A legend is essential to distinguish between data.
In ggplot2
, we can build overlapping bar graphs using the fill
argument in geom_bar()
or geom_col()
10.2 Set up
PACKAGES:
Install packages.
show/hide
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
Remove missing species
from penguins
and filter the data to only penguins on "Dream"
island.
show/hide
<- filter(penguins,
penguins_ovrlp !is.na(species) &
== "Dream")
island glimpse(penguins_ovrlp)
#> Rows: 124
#> Columns: 8
#> $ species <fct> Adelie, Adelie, Adelie…
#> $ island <fct> Dream, Dream, Dream, D…
#> $ bill_length_mm <dbl> 39.5, 37.2, 39.5, 40.9…
#> $ bill_depth_mm <dbl> 16.7, 18.1, 17.8, 18.9…
#> $ flipper_length_mm <int> 178, 178, 188, 184, 19…
#> $ body_mass_g <int> 3250, 3900, 3300, 3900…
#> $ sex <fct> female, male, female, …
#> $ year <int> 2007, 2007, 2007, 2007…
10.3 Grammar
CODE:
Create labels with
labs()
Initialize the graph with
ggplot()
and providedata
Map
flipper_length_mm
to thex
andspecies
tofill
Add the
geom_bar()
layer
show/hide
<- labs(
labs_bar_ovrlp title = "Adult foraging penguins on Dream island",
x = "Flipper length (mm)",
y = "Count",
fill = "Species")
<- ggplot(data = penguins_ovrlp,
ggp2_bar_ovrlp aes(x = flipper_length_mm, fill = species)) +
geom_bar()
+
ggp2_bar_ovrlp labs_bar_ovrlp
GRAPH:
10.4 More info
Overlapping bar graphs can also be built with
geom_col()
.geom_bar()
has additional options for arranging overlapping bars. We can set theposition
argument to"dodge"
or"dodge2"
, depending on how we’d like the data displayed.
10.4.1 geom_col()
To build an overlapping bar graph with geom_col()
, we need to create a column with the counts for flipper_length_mm
and species
in the dataset.
- Create the
penguins_col
data:
show/hide
<- penguins_ovrlp |>
penguins_col count(species, flipper_length_mm, name = "Count")
- Map the counts to the
y
axis,flipper_length_mm
to thex
axis, andspecies
tofill
show/hide
<- labs(
labs_col_ovrlp title = "Adult foraging penguins on Dream island",
subtitle = "built with 'geom_col()'",
x = "Flipper length (mm)",
y = "Count",
fill = "Species")
<- ggplot(data = penguins_col,
ggp2_col_ovrlp mapping = aes(y = Count,
x = flipper_length_mm,
fill = species)) +
geom_col()
+
ggp2_col_ovrlp labs_col_ovrlp
Compare the two graphs below:
10.4.2 dodge
position = "dodge"
preserves the vertical position of a geom while adjusting the horizontal position"dodge"
requires the grouping variable to be be specified in the global orgeom_
layer
Create the penguins_dodge
data.
show/hide
<- filter(penguins,
penguins_dodge !is.na(species) &
!is.na(sex) &
== "Dream")
island glimpse(penguins_dodge)
#> Rows: 123
#> Columns: 8
#> $ species <fct> Adelie, Adelie, Adelie…
#> $ island <fct> Dream, Dream, Dream, D…
#> $ bill_length_mm <dbl> 39.5, 37.2, 39.5, 40.9…
#> $ bill_depth_mm <dbl> 16.7, 18.1, 17.8, 18.9…
#> $ flipper_length_mm <int> 178, 178, 188, 184, 19…
#> $ body_mass_g <int> 3250, 3900, 3300, 3900…
#> $ sex <fct> female, male, female, …
#> $ year <int> 2007, 2007, 2007, 2007…
Create labels with
labs()
Initialize the graph with
ggplot()
and providedata
Map
species
to thex
andisland
togroup
andfill
Inside the
geom_bar()
function, setposition
to"dodge"
show/hide
<- labs(
labs_bar_dodge title = "Adult foraging penguins on Dream island",
subtitle = "position = 'dodge'",
x = "Sex",
y = "Count",
fill = "Species")
<- ggplot(data = penguins_dodge,
ggp2_bar_dodge aes(x = sex,
group = species,
fill = species)) +
geom_bar(
position = "dodge")
+
ggp2_bar_dodge labs_bar_dodge
10.4.3 dodge2
Create labels with
labs()
Initialize the graph with
ggplot()
and providedata
Map
species
tox
andisland
tofill
Inside
geom_bar()
, setposition
to"dodge2"
"dodge2"
works without a grouping variable in a layer"dodge2"
works with bars and rectangles"dodge2"
is useful for arranging graphs with variable widths.
show/hide
<- labs(
labs_bar_dodge2 title = "Adult foraging penguins on Dream island",
subtitle = "position = 'dodge2'",
x = "Sex",
y = "Count",
fill = "Species")
<- ggplot(data = penguins_dodge,
ggp2_bar_dodge2 aes(x = sex,
fill = species)) +
geom_bar(
position = "dodge2")
+
ggp2_bar_dodge2 labs_bar_dodge2
Compare the two graphs below: