
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
penguins_ovrlp <- filter(penguins,
!is.na(species) &
island == "Dream")
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 providedataMap
flipper_length_mmto thexandspeciestofillAdd the
geom_bar()layer
show/hide
labs_bar_ovrlp <- labs(
title = "Adult foraging penguins on Dream island",
x = "Flipper length (mm)",
y = "Count",
fill = "Species")
ggp2_bar_ovrlp <- ggplot(data = penguins_ovrlp,
aes(x = flipper_length_mm, fill = species)) +
geom_bar()
ggp2_bar_ovrlp +
labs_bar_ovrlpGRAPH:

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 thepositionargument 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_coldata:
show/hide
penguins_col <- penguins_ovrlp |>
count(species, flipper_length_mm, name = "Count")- Map the counts to the
yaxis,flipper_length_mmto thexaxis, andspeciestofill
show/hide
labs_col_ovrlp <- labs(
title = "Adult foraging penguins on Dream island",
subtitle = "built with 'geom_col()'",
x = "Flipper length (mm)",
y = "Count",
fill = "Species")
ggp2_col_ovrlp <- ggplot(data = penguins_col,
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
penguins_dodge <- filter(penguins,
!is.na(species) &
!is.na(sex) &
island == "Dream")
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 providedataMap
speciesto thexandislandtogroupandfillInside the
geom_bar()function, setpositionto"dodge"
show/hide
labs_bar_dodge <- labs(
title = "Adult foraging penguins on Dream island",
subtitle = "position = 'dodge'",
x = "Sex",
y = "Count",
fill = "Species")
ggp2_bar_dodge <- ggplot(data = penguins_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 providedataMap
speciestoxandislandtofillInside
geom_bar(), setpositionto"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_bar_dodge2 <- labs(
title = "Adult foraging penguins on Dream island",
subtitle = "position = 'dodge2'",
x = "Sex",
y = "Count",
fill = "Species")
ggp2_bar_dodge2 <- ggplot(data = penguins_dodge,
aes(x = sex,
fill = species)) +
geom_bar(
position = "dodge2")
ggp2_bar_dodge2 +
labs_bar_dodge2
Compare the two graphs below:

