Hexagon bins
Description
Hexagon bins (or hex-bins) are a very similar to 2-D histograms, but instead of dividing the graph area into a grid, it’s divided into hexagons. The number of points inside each per hexagon determine it’s color.
Getting set up
PACKAGES:
Install packages.
Code
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
We’ll take the flipper_length_mm
, bill_length_mm
, bill_depth_mm
, species
, sex
, and island
variables from palmerpenguins::penguins
and drop the missing values.
Code
<- palmerpenguins::penguins |>
penguins_hex ::select(flipper_length_mm, bill_depth_mm,
dplyr|>
bill_length_mm, species, sex, island) ::drop_na()
tidyrglimpse(penguins_hex)
Rows: 333
Columns: 6
$ flipper_length_mm <int> 181, 186, 195, 193, 190, 181, 195, 182, 191, 198, 18…
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, 19.3, 20.6, 17.8, 19.6, 17.6, 21.2…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, 36.7, 39.3, 38.9, 39.2, 41.1, 38.6…
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ sex <fct> male, female, female, female, male, female, male, fe…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
The grammar
CODE:
Create labels with labs()
Initialize the graph with ggplot()
and provide data
Map bill_length_mm
to the x
and flipper_length_mm
to the y
Add the geom_hex()
layer
Code
<- labs(
labs_hex title = "Adult Foraging Penguins",
subtitle = "Near Palmer Station, Antarctica",
x = "Bill length (mm)",
y = "Flipper length (mm)")
# graph
<- ggplot(data = penguins_hex,
ggp2_hex aes(x = bill_length_mm, y = flipper_length_mm)) +
geom_hex()
+
ggp2_hex labs_hex
GRAPH:
More info
BINS:
Below we change the bins
to 20
and 15
and save these layers as ggp2_hex_b20
and ggp2_hex_b15
.
Decreasing the number of bins
increases the size of the hexagons (and makes them larger).
Code
<- ggplot(data = penguins_hex,
ggp2_hex_b20 aes(x = bill_length_mm, y = flipper_length_mm)) +
geom_hex(bins = 20)
+
ggp2_hex_b20
labs_hex<- ggplot(data = penguins_hex,
ggp2_hex_b15 aes(x = bill_length_mm, y = flipper_length_mm)) +
geom_hex(bins = 15)
+
ggp2_hex_b15 labs_hex
SCALES:
We can adjust the color scale using scale_color_discrete_sequential()
and setting aesthetics
to "fill"
.
If the hexagons overlap, we can use the alpha
to make them slightly transparent.
Code
<- labs(
labs_hex2 title = "Adult Foraging Penguins",
subtitle = "Near Palmer Station, Antarctica",
x = "Bill length (mm)",
y = "Flipper length (mm)",
fill = "Sex")
ggplot(data = penguins_hex,
aes(x = bill_length_mm,
y = flipper_length_mm)) +
geom_hex(aes(fill = sex),
bins = 15,
alpha = 3/4) +
scale_color_discrete_sequential(
aesthetics = "fill",
rev = FALSE,
palette = "Viridis") +
labs_hex2
Get a full list of available color palette’s using hcl_palettes(type =
"sequential")
OPTIONS:
binwidth
allows us to manually adjust the size of the hexagons.
linewidth
is also helpful when using alpha
for overlapping values.
Code
<- labs(
labs_hex3 title = "Adult Foraging Penguins",
subtitle = "Near Palmer Station, Antarctica",
x = "Bill length (mm)",
y = "Flipper length (mm)",
fill = "Species")
ggplot(data = penguins_hex,
aes(x = bill_length_mm,
y = flipper_length_mm,
fill = species)) +
geom_hex(binwidth = c(1.1, 3),
linewidth = 0.5,
alpha = 3/4,
color = "#000000") +
scale_color_discrete_sequential(
aesthetics = "fill",
palette = "Dark Mint") +
labs_hex3
Bins can be set with bins
(a single number) or binwidth
(a numeric vector of c(x, y)
)