---
title: "Hexagon bins"
format:
html:
toc: true
toc-location: right
toc-title: Contents
code-fold: true
out-height: '100%'
out-width: '100%'
execute:
warning: false
message: false
---
```{r}
#| label: setup
#| message: false
#| warning: false
#| include: false
library (tidyverse)
library (lubridate)
library (scales)
library (knitr)
library (kableExtra)
library (colorblindr)
library (downlit)
# options ----
options (
repos = "https://cloud.r-project.org" ,
dplyr.print_min = 6 ,
dplyr.print_max = 6 ,
scipen = 9999 )
# fonts ----
library (extrafont)
library (sysfonts)
# import font
extrafont:: font_import (
paths = "assets/Ubuntu/" ,
prompt = FALSE )
# add font
sysfonts:: font_add (
family = "Ubuntu" ,
regular = "assets/Ubuntu/Ubuntu-Regular.ttf" )
# use font
showtext:: showtext_auto ()
# add theme
source ("R/theme_ggp2g.R" )
# set theme
ggplot2:: theme_set (theme_ggp2g (
base_size = 15 ))
install.packages ("palmerpenguins" )
```
:::: {.callout-note collapse="false" icon=false}
## Graph info
::: {style="font-size: 1.25em; color: #02577A;"}
**Should I use this graph?**
:::
<br>
```{r}
#| label: full_code_display
#| eval: true
#| echo: false
#| warning: false
#| message: false
#| out-height: '60%'
#| out-width: '60%'
#| fig-align: right
library (palmerpenguins)
library (ggplot2)
penguins_hex <- palmerpenguins:: penguins |>
dplyr:: select (flipper_length_mm, bill_depth_mm,
bill_length_mm, species, sex, island) |>
tidyr:: drop_na ()
labs_hex <- labs (
title = "Adult Foraging Penguins" ,
subtitle = "Near Palmer Station, Antarctica" ,
x = "Bill length (mm)" ,
y = "Flipper length (mm)" )
ggp2_hex_b15 <- ggplot (data = penguins_hex,
aes (x = bill_length_mm, y = flipper_length_mm)) +
geom_hex (bins = 15 )
ggp2_hex_b15 +
labs_hex
```
::: {style="font-size: 1.10em; color: #02577A;"}
**This graph requires:**
:::
::: {style="font-size: 0.90em; color: #043b67;"}
`r emo::ji("check")` two numeric (continuous) variable
:::
::::
## 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
:::: {.panel-tabset}
### Packages
::: {style="font-size: 1.15em; color: #1e83c8;"}
**PACKAGES:**
:::
::: {style="font-size: 0.85em;"}
Install packages.
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: pkg_code_hex
#| code-fold: show
#| eval: true
#| echo: true
#| warning: false
#| message: false
#| results: hide
install.packages ("palmerpenguins" )
library (palmerpenguins)
library (ggplot2)
```
:::
### Data
::: {style="font-size: 1.15em; color: #1e83c8;"}
**DATA:**
:::
::: {.column-margin}
 {fig-align="right" width="100%" height="100%"}
:::
<!--
{fig-align="right" width="15%" height="15%"}
{fig-align="right" width="20%" height="20%"}
-->
::: {style="font-size: 0.85em;"}
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.
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: data_code_hex
#| eval: true
#| echo: true
penguins_hex <- palmerpenguins:: penguins |>
dplyr:: select (flipper_length_mm, bill_depth_mm,
bill_length_mm, species, sex, island) |>
tidyr:: drop_na ()
glimpse (penguins_hex)
```
:::
::::
## The grammar
:::: {.panel-tabset}
### Code
::: {style="font-size: 1.15em; color: #1e83c8;"}
**CODE:**
:::
::: {style="font-size: 0.85em;"}
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
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: code_graph_hex
#| code-fold: show
#| eval: false
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| column: page-inset-right
#| layout-nrow: 1
labs_hex <- labs (
title = "Adult Foraging Penguins" ,
subtitle = "Near Palmer Station, Antarctica" ,
x = "Bill length (mm)" ,
y = "Flipper length (mm)" )
# graph
ggp2_hex <- ggplot (data = penguins_hex,
aes (x = bill_length_mm, y = flipper_length_mm)) +
geom_hex ()
ggp2_hex +
labs_hex
```
:::
### Graph
::: {style="font-size: 1.15em; color: #1e83c8;"}
**GRAPH:**
:::
```{r}
#| label: create_graph_hex
#| eval: true
#| echo: false
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| column: page-inset-right
#| layout-nrow: 1
labs_hex <- labs (
title = "Adult Foraging Penguins" ,
subtitle = "Near Palmer Station, Antarctica" ,
x = "Bill length (mm)" ,
y = "Flipper length (mm)" )
# graph
ggp2_hex <- ggplot (data = penguins_hex,
aes (x = bill_length_mm, y = flipper_length_mm)) +
geom_hex ()
ggp2_hex +
labs_hex
```
::::
## More info
:::: {.panel-tabset}
### Bins
::: {style="font-size: 1.15em; color: #1e83c8;"}
**BINS:**
:::
::: {style="font-size: 0.85em;"}
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).
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: bins_hex
#| eval: true
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| column: page-inset-right
#| layout-nrow: 1
ggp2_hex_b20 <- ggplot (data = penguins_hex,
aes (x = bill_length_mm, y = flipper_length_mm)) +
geom_hex (bins = 20 )
ggp2_hex_b20 +
labs_hex
ggp2_hex_b15 <- ggplot (data = penguins_hex,
aes (x = bill_length_mm, y = flipper_length_mm)) +
geom_hex (bins = 15 )
ggp2_hex_b15 +
labs_hex
```
:::
### Scale
::: {style="font-size: 1.15em; color: #1e83c8;"}
**SCALES:**
:::
::: {style="font-size: 0.85em;"}
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.
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: scales_2d_hex
#| eval: true
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| column: page-inset-right
#| layout-nrow: 1
labs_hex2 <- labs (
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
```
::: {style="font-size: 0.85em;"}
Get a full list of available color palette's using `hcl_palettes(type = ` `"sequential")`
:::
:::
### Options
::: {style="font-size: 1.15em; color: #1e83c8;"}
**OPTIONS:**
:::
::: {style="font-size: 0.85em;"}
`binwidth` allows us to manually adjust the size of the hexagons.
`linewidth` is also helpful when using `alpha` for overlapping values.
:::
::: {style="font-size: 0.75em;"}
```{r}
#| label: pals_2d_hex
#| eval: true
#| echo: true
#| warning: false
#| message: false
#| out-height: '100%'
#| out-width: '100%'
#| column: page-inset-right
#| layout-nrow: 1
labs_hex3 <- labs (
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
```
:::
::: {style="font-size: 0.85em;"}
Bins can be set with `bins` (a single number) or `binwidth` (a numeric vector of `c(x, y)` )
:::
::::