Created
July 16, 2023 21:22
-
-
Save loukesio/6cb6f2ed1514cab9e11b3a0f06d1f64d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load required packages | |
| library(tidyverse) | |
| library(gridExtra) | |
| library(showtext) | |
| library(grid) | |
| # Read the global temperature data from CSV file | |
| global_temps <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-07-11/global_temps.csv') | |
| # Add custom font and enable automatic text rendering | |
| font_add_google("Lato", "my_lato") | |
| showtext_auto() | |
| # Perform data transformations and create the temperature plot | |
| temp <- global_temps %>% | |
| select(Year:Dec) %>% | |
| rowwise() %>% | |
| mutate(min_value = min(c_across(Jan:Dec), na.rm = TRUE), | |
| max_value = max(c_across(Jan:Dec), na.rm = TRUE), | |
| mean_value = mean(c_across(Jan:Dec), na.rm = TRUE)) %>% | |
| select(1, min_value, max_value, mean_value) %>% | |
| pivot_longer(!Year) %>% | |
| filter(Year > 1986) %>% | |
| ggplot() + | |
| geom_line(aes(x = value, y = Year, group = Year), color = "#333333") + | |
| geom_point(aes(x = value, y = Year, color = name), size = 3) + | |
| scale_color_manual( | |
| values = c(min_value = "#00798c", mean_value = "#e3d5d5", max_value = "#d1495b"), | |
| breaks = c("min_value", "mean_value", "max_value"), | |
| labels = c("min Temp", "mean Temp", "max Temp"), | |
| name = "" | |
| ) + | |
| scale_y_continuous(breaks = seq(1987, 2023, by = 5)) + | |
| guides(alpha = "none") + | |
| scale_x_continuous( | |
| breaks = seq(0, 1.50, 0.25), | |
| limits = c(-0.1, 1.50) | |
| ) + | |
| coord_cartesian( | |
| xlim = c(0, 1.6), | |
| ylim = c(1986, 2024), | |
| expand = FALSE, | |
| clip = 'off' | |
| ) + | |
| annotate( | |
| 'segment', x = 0, xend = 1.5, | |
| y = 1986, | |
| yend = 1986, | |
| col = "#333333", | |
| size = 0.25 | |
| ) + | |
| labs( | |
| x = "Temperature (°C)", y = "Year", | |
| title = "Riding the Thermal Waves: A Journey from 1987 to Today", | |
| subtitle = "Depicting changes in global temperatures over time: Minimum, Mean, and Maximum\nvalues from 1987 onwards" | |
| ) + | |
| theme_minimal() + | |
| theme( | |
| text = element_text(family = "my_lato", color = "#333333"), | |
| panel.grid.major.y = element_blank(), | |
| panel.grid.minor.y = element_blank(), | |
| panel.grid.major.x = element_blank(), | |
| panel.grid.minor.x = element_blank(), | |
| plot.title = element_text(hjust = 0.5, margin = margin(10, 0, 0, 0), size = 15), | |
| plot.subtitle = element_text(family = "my_lato", size = 11, color = "#333333", hjust = 0.5), | |
| legend.position = "bottom", | |
| axis.title.x = element_text(size = 13, margin = margin(10, 0, 0, 0)), | |
| axis.text.x = element_text(size = 12), | |
| axis.ticks.x = element_line(color = "#333333", size = 0.3), | |
| axis.ticks.length = unit(.2, "cm"), | |
| axis.line.x = element_blank(), | |
| axis.title.y = element_text(size = 13, margin = margin(0, 10, 0, 0)), | |
| axis.text.y = element_text(size = 12) | |
| ) | |
| # Display the temperature plot | |
| temp | |
| # Add a rectangle grob for additional styling | |
| rect_grob <- rectGrob( | |
| width = 0.01, | |
| height = 0.08, | |
| gp = gpar(fill = "#d1495b", col = "transparent") | |
| ) | |
| # Draw the rectangle grob on the plot | |
| grid.draw(grobTree(rect_grob, vp = viewport(x = 0.1, y = 0.93))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment