This is a series of five posts for this data:
Introduction
A member of the Hamilton Naturalist Club mentioned the idea of creating a gganimate
plot, so I thought I’d try it! I mostly modified the code from this StackOverflow page.
There’s some jumping around, and I think a way to prevent that might be to interpolate values between each year so that, say, Lesser Scaup doesn’t go from not-on-the-chart in one year to 2002 individuals seen in the next year.
Data visualizing using a gganimate plot
Here is the final plot:

Final gif of the top bird counts since 1955
And here is the code that I used to create it!
library(dplyr)
library(here)
library(readr)
library(gganimate)
library(ggplot2)
library(gifski)
hamilton_cbc <- read_rds(here("content",
"post",
"2019-03-24-hamilton-cbc-part-3",
"hamilton_cbc_shiny",
"hamilton_cbc_output_part_2.rds"))
theme_set(theme_minimal())
# Every year had at least 9 species
hamilton_cbc %>%
group_by(year) %>%
filter(how_many_counted > 0) %>%
summarise(n_counted = n()) %>%
arrange(n_counted)
hamilton_cbc_ranked <- hamilton_cbc %>%
group_by(year) %>%
# The * 1 makes it possible to have non-integer ranks while sliding (because it makes rank a double, not integer variable)
arrange(year, -(how_many_counted), species) %>%
mutate(rank = row_number() * 1) %>%
ungroup() %>%
filter(rank < 8,
year >= 1955)
p <- hamilton_cbc_ranked %>%
ggplot(aes(rank, group = species,
fill = as.factor(species), color = as.factor(species))) +
geom_tile(aes(y = how_many_counted / 2,
height = how_many_counted,
width = 0.9), alpha = 0.8, color = NA) +
# text in x-axis (requires clip = "off" in coord_*)
# paste(country, " ") is a hack to make pretty spacing, since hjust > 1
# leads to weird artifacts in text spacing.
geom_text(aes(y = 0, label = paste(species, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y = how_many_counted, label = paste0(" ", how_many_counted), hjust = 0)) + # value label thanks to Nitish
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
labs(title='{closest_state}', x = "", y = "Number of birds counted") +
theme(plot.title = element_text(hjust = 0, size = 22),
axis.ticks.y = element_blank(), # These relate to the axes post-flip
axis.text.y = element_blank(), # These relate to the axes post-flip
plot.margin = margin(1,1,1,4, "cm")) +
transition_states(year, transition_length = 4, state_length = 1) +
ease_aes('cubic-in-out')
# Have to install the gifski package in order for the renderer to work
# Make fps 25 to make it smoother
# Duration is how many centiseconds between years
animate(p, fps = 25, duration = 80, width = 800, height = 600, renderer = gifski_renderer())
And thank you to the Christmas Bird Count! The Christmas Bird Count Data was provided by National Audubon Society and through the generous efforts of Bird Studies Canada and countless volunteers across the western hemisphere.
Session info
- Session info ---------------------------------------------------------------
setting value
version R version 4.0.2 (2020-06-22)
os Windows 10 x64
system x86_64, mingw32
ui RTerm
language (EN)
collate English_Canada.1252
ctype English_Canada.1252
tz America/New_York
date 2020-09-04
- Packages -------------------------------------------------------------------
! package * version date lib source
P assertthat 0.2.1 2019-03-21 [?] CRAN (R 4.0.0)
P backports 1.1.8 2020-06-17 [?] CRAN (R 4.0.0)
P blogdown 0.20 2020-06-23 [?] CRAN (R 4.0.2)
P bookdown 0.20 2020-06-23 [?] CRAN (R 4.0.0)
P callr 3.4.3 2020-03-28 [?] CRAN (R 4.0.0)
P cli 2.0.2 2020-02-28 [?] CRAN (R 4.0.0)
P colorspace 1.4-1 2019-03-18 [?] CRAN (R 4.0.0)
P crayon 1.3.4 2017-09-16 [?] CRAN (R 4.0.0)
P desc 1.2.0 2018-05-01 [?] CRAN (R 4.0.0)
P devtools * 2.3.1 2020-07-21 [?] CRAN (R 4.0.2)
P digest 0.6.25 2020-02-23 [?] CRAN (R 4.0.0)
P dplyr * 1.0.1 2020-07-31 [?] CRAN (R 4.0.2)
P ellipsis 0.3.1 2020-05-15 [?] CRAN (R 4.0.2)
P emo * 0.0.0.9000 2020-07-07 [?] Github (hadley/[email protected])
P evaluate 0.14 2019-05-28 [?] CRAN (R 4.0.0)
P fansi 0.4.1 2020-01-08 [?] CRAN (R 4.0.0)
P farver 2.0.3 2020-01-16 [?] CRAN (R 4.0.0)
P fs 1.5.0 2020-07-31 [?] CRAN (R 4.0.2)
P generics 0.0.2 2018-11-29 [?] CRAN (R 4.0.0)
P gganimate * 1.0.6 2020-07-08 [?] CRAN (R 4.0.2)
P ggplot2 * 3.3.2 2020-06-19 [?] CRAN (R 4.0.2)
P gifski 0.8.6 2018-09-28 [?] CRAN (R 4.0.2)
P glue 1.4.1 2020-05-13 [?] CRAN (R 4.0.2)
P gtable 0.3.0 2019-03-25 [?] CRAN (R 4.0.0)
P here * 0.1 2017-05-28 [?] CRAN (R 4.0.2)
P hms 0.5.3 2020-01-08 [?] CRAN (R 4.0.0)
P htmltools 0.5.0 2020-06-16 [?] CRAN (R 4.0.2)
P knitr 1.29 2020-06-23 [?] CRAN (R 4.0.2)
P lifecycle 0.2.0 2020-03-06 [?] CRAN (R 4.0.0)
P lubridate 1.7.9 2020-06-08 [?] CRAN (R 4.0.2)
P magrittr 1.5 2014-11-22 [?] CRAN (R 4.0.0)
P memoise 1.1.0 2017-04-21 [?] CRAN (R 4.0.0)
P munsell 0.5.0 2018-06-12 [?] CRAN (R 4.0.0)
P pillar 1.4.6 2020-07-10 [?] CRAN (R 4.0.2)
P pkgbuild 1.1.0 2020-07-13 [?] CRAN (R 4.0.2)
P pkgconfig 2.0.3 2019-09-22 [?] CRAN (R 4.0.0)
P pkgload 1.1.0 2020-05-29 [?] CRAN (R 4.0.2)
P prettyunits 1.1.1 2020-01-24 [?] CRAN (R 4.0.0)
P processx 3.4.3 2020-07-05 [?] CRAN (R 4.0.2)
P progress 1.2.2 2019-05-16 [?] CRAN (R 4.0.0)
P ps 1.3.4 2020-08-11 [?] CRAN (R 4.0.2)
P purrr 0.3.4 2020-04-17 [?] CRAN (R 4.0.0)
P R6 2.4.1 2019-11-12 [?] CRAN (R 4.0.0)
P Rcpp 1.0.5 2020-07-06 [?] CRAN (R 4.0.2)
P readr * 1.3.1 2018-12-21 [?] CRAN (R 4.0.0)
P remotes 2.2.0 2020-07-21 [?] CRAN (R 4.0.2)
renv 0.11.0 2020-06-26 [1] CRAN (R 4.0.2)
P rlang 0.4.7 2020-07-09 [?] CRAN (R 4.0.2)
P rmarkdown 2.3 2020-06-18 [?] CRAN (R 4.0.2)
P rprojroot 1.3-2 2018-01-03 [?] CRAN (R 4.0.0)
P scales 1.1.1 2020-05-11 [?] CRAN (R 4.0.2)
P sessioninfo 1.1.1 2018-11-05 [?] CRAN (R 4.0.0)
P stringi 1.4.6 2020-02-17 [?] CRAN (R 4.0.0)
P stringr 1.4.0 2019-02-10 [?] CRAN (R 4.0.0)
P testthat 2.3.2 2020-03-02 [?] CRAN (R 4.0.0)
P tibble 3.0.3 2020-07-10 [?] CRAN (R 4.0.2)
P tidyselect 1.1.0 2020-05-11 [?] CRAN (R 4.0.2)
P tweenr 1.0.1 2018-12-14 [?] CRAN (R 4.0.2)
P usethis * 1.6.1 2020-04-29 [?] CRAN (R 4.0.2)
P vctrs 0.3.2 2020-07-15 [?] CRAN (R 4.0.2)
P withr 2.2.0 2020-04-20 [?] CRAN (R 4.0.0)
P xfun 0.16 2020-07-24 [?] CRAN (R 4.0.2)
P yaml 2.2.1 2020-02-01 [?] CRAN (R 4.0.0)
[1] C:/Users/shw/Desktop/blog2/renv/library/R-4.0/x86_64-w64-mingw32
[2] C:/Users/shw/AppData/Local/Temp/RtmpwncJ8N/renv-system-library
P -- Loaded and on-disk path mismatch.