---
title: "Visualizing Australian climate data over time"
description: "Putting Australia's extreme climate conditions in 2019 into historical context"
author:
- name: Mickey Rafa
url: https://mrafa3.github.io/
date: 09-23-2020
categories: [R, "#TidyTuesday", line-plot] # self-defined categories
image: "tt_2020_2_thumbnail.png"
draft: false # setting this to `true` will prevent your post from appearing on your listing page until you're ready!
format:
html:
toc: true
toc-depth: 5
code-link: true
code-fold: true
code-tools: true
code-summary: "Show code"
self-contained: true
editor_options:
chunk_output_type: inline
execute:
error: false
message: false
warning: false
eval: true
---
.](tt_2020_2.png){#fig-1 width="100%"}
# 1. Load Packages & Setup
```{r setup, include=TRUE}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse,
tidytuesdayR,
dlookr,
ggtext,
ggforce,
showtext,
janitor, #for clean_names()
scales,
glue,
here,
patchwork,
zoo
)
font_add('fa-brands', 'fonts/Font Awesome 6 Brands-Regular-400.otf')
font_add_google("Roboto")
font_add_google("Roboto Condensed")
```
# 2. Read in the Data
```{r read_data, include=TRUE}
tt_year <- 2020
tt_week <- 2
tuesdata <- tidytuesdayR::tt_load(tt_year, week = tt_week)
rainfall <- tuesdata$rainfall
temperature <- tuesdata$temperature
```
# 3. Examine the Data
```{r examine, include=TRUE, echo=TRUE}
rainfall %>%
glimpse()
temperature %>%
glimpse()
```
# 4. Tidy the Data
```{r annualize, include=TRUE}
annual_rainfall <- rainfall %>%
group_by(year, month) %>%
summarize(avg_monthly_rainfall = mean(rainfall, na.rm = TRUE),
.groups = 'drop') %>%
group_by(year) %>%
summarize(annual_rainfall = sum(avg_monthly_rainfall, na.rm = TRUE),
.groups = 'drop')
annual_temperature <- temperature %>%
filter(temp_type == 'max') %>%
mutate(year = year(date)) %>%
group_by(year) %>%
summarise(avg_temperature = mean(temperature, na.rm = TRUE),
.groups = 'drop') %>%
filter(year < 2020)
```
# 5. Visualization Parameters
```{r my_theme, include=TRUE}
my_theme <- theme(
plot.title = element_text(family="Roboto", color="black", face="bold", size=48, hjust=0),
plot.subtitle = element_text(family="Roboto", color="black", size=34, hjust=0),
axis.title = element_text(family="Roboto", color="black", face="bold", size=16),
axis.text = element_text(family="Roboto", color="black", size=28),
axis.ticks = element_blank(),
plot.caption = element_text(family="Roboto", color="black", size=24),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "grey90", linewidth = 0.5),
panel.grid.minor = element_line(colour = "grey93", linewidth = 0.5),
panel.border = element_blank(),
legend.title=element_blank(),
legend.text = element_text(family="Roboto", color="black", size=12, hjust=0),
legend.position = 'top',
)
```
# 6. Plot
```{r plot_rain_temp, fig.width=10, fig.height=6, fig.align='center'}
plot_rainfall <- annual_rainfall %>%
filter(year >= 1900 & year < 2020) %>%
mutate(ma10 = zoo::rollapply(annual_rainfall, width=10, FUN=mean, align='right', partial = TRUE)) %>%
ggplot(.,
aes(x=year,
y=annual_rainfall)) +
geom_line(color = 'cornflowerblue') +
geom_point(color = 'cornflowerblue') +
geom_line(aes(y=ma10),
linetype='longdash', color='gray30') +
geom_mark_circle(aes(filter = year == 2019,
label = glue("Total Rainfall: {round(annual_rainfall,1)} mm"),
description = "Annual rainfall at a 60 year low"),
label.family = c("Roboto", "Roboto Condensed"), label.buffer = unit(6, "cm"), label.fontsize = 18) +
scale_x_continuous(limits = c(1900, 2020), breaks = seq(1900, 2020, 20)) +
labs(x='',
y='') +
scale_y_continuous(limits = c(10, 50),
breaks = seq(10, 50, 10),
labels = scales::label_number(suffix = " mm")) +
my_theme
plot_temperature <- annual_temperature %>%
mutate(ma10 = zoo::rollapply(avg_temperature, width=10, FUN=mean, align='right', partial = TRUE)) %>%
ggplot(.,
aes(x=year,
y=avg_temperature)) +
geom_line(color = 'firebrick3') +
geom_point(color = 'firebrick3') +
geom_line(aes(y=ma10),
linetype='longdash', color='gray30') +
geom_mark_circle(aes(filter = year == 2019,
label = glue("Avg Max Temp: {round(avg_temperature, 1)}°C"),
description = "Data through May 2019"),
label.family = c("Roboto", "Roboto Condensed"), label.buffer = unit(6, "cm"), label.fontsize = 18) +
labs(x='',
y='') +
scale_x_continuous(limits = c(1900, 2020), breaks = seq(1900, 2020, 20)) +
# scale_y_continuous(limits = c(10, 30), breaks = seq(10, 30, 10), label = label_number(unit = "° C", sep = ""), position = "right") +
scale_y_continuous(limits = c(10, 30),
breaks = seq(10, 30, 10),
labels = scales::label_number(suffix = "°C"),
position = "right") +
my_theme
plot_rain_temp <- plot_rainfall +
plot_temperature +
theme(plot.margin = margin(r = 30, l = 30)) +
plot_annotation(
title = "Historic Lows in Rainfall and Records High Temperatures a Factor in Australia's Devastating Wildfires",
subtitle = glue(
"<span style='color:cornflowerblue'>**Total rainfall**</span> and <span style='color:firebrick3'>**average maximum temperature**</span> across Australian cities from 1900 to 2019"
),
caption = "Tidy Tuesday Week 2 (2020). **Source**: Australian Bureau of Meterology",
theme = theme(
plot.title = element_textbox_simple(family='Roboto', size=48, face='bold'),
plot.subtitle = element_textbox_simple(family='Roboto', size=34),
plot.caption = element_textbox_simple(family = 'Roboto', size=18)
)
)
```
# 7. Save
```{r save_plot, include=TRUE}
# Save the plot as PNG
ggsave(
filename = glue("tt_{tt_year}_{tt_week}.png"),
plot = plot_rain_temp,
width = 10, height = 6, units = "in", dpi = 320
)
# make thumbnail for page
magick::image_read(glue("tt_{tt_year}_{tt_week}.png")) %>%
magick::image_resize(geometry = "400") %>%
magick::image_write(glue("tt_{tt_year}_{tt_week}_thumbnail.png"))
```
# 8. Session Info
::: {.callout-tip collapse="true"}
##### Expand for Session Info
```{r, echo = FALSE}
sessionInfo()
```
:::
# 9. Github Repository
::: {.callout-tip collapse="true"}
##### Expand for GitHub Repo
[Access the GitHub repository here](https://github.com/mrafa3/mrafa3.github.io)
:::