Skip to content

Instantly share code, notes, and snippets.

@chelseaparlett
Created February 27, 2022 00:30
Show Gist options
  • Save chelseaparlett/8a4300a281ba870f4f757b51afe8ffc2 to your computer and use it in GitHub Desktop.
Save chelseaparlett/8a4300a281ba870f4f757b51afe8ffc2 to your computer and use it in GitHub Desktop.
library(readr)
library(tidyverse)
# read in data
urlfile <- "https://raw.githubusercontent.com/Z-Unlocked/Unlocked_Challenge_1/main/temperature_change_data_11-29-2021.csv"
mydata <-read_csv(url(urlfile))
# filter
countries <- c("China", "Germany", "Japan", "United States of America")
glimpse(mydata)
df <- mydata %>% filter(`Flag Description` != "Data Not Available" &
Months == "Meteorological year" &
Area %in% countries) %>%
group_by(Area, Year) %>% summarise(Value = mean(Value))
# plot
annotation <- data.frame(
x = c(1962,1962),
y = c(1.5, -0.5),
label = c("Increase", "Decrease")
)
ggplot(df, aes(x = Year, y = Value, color = Area, linetype = Area)) +
geom_line(size = 1) +
theme_minimal() +
labs(y = "Change In Temperature",
title = "Temperature Change for Top 4 Economies (in 2021)",
subtitle = "(from 1951-1980 baseline)",
x = "") +
theme(panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
axis.title.y = element_text(size = 15),
axis.title.x = element_text(size = 15),
axis.text.x = element_text(size = 12),
axis.text.y = element_text(size = 12),
plot.title = element_text(size = 18, hjust = 0.5),
plot.subtitle = element_text(size = 12,hjust = 0.5),
legend.position = "bottom",
legend.title=element_text(size=12),
legend.text = element_text(size = 12),
text=element_text(family="sans")) +
geom_hline(yintercept = 0, size = 1, alpha = 0.2) +
scale_color_manual(values =c("#8CB369", "#437000", "#F7BC50", "#B1702F")) +
geom_segment(x = 1960, xend = 1960, y = 0.1, yend = 2.5,
color = "#BC4B51", size = 0.5,
arrow = arrow(length = unit(0.25, "cm")),
show.legend = FALSE,
alpha = 0.25) +
geom_segment(x = 1960, xend = 1960, y = -0.1, yend = -1,
color = "#5B8E7D", size = 0.5,
arrow = arrow(length = unit(0.25, "cm")),
show.legend = FALSE,
alpha = 0.25) +
annotate(geom = "text", x = 1959.3, y = 1.5, label = "Increase",
angle = 90, color = "#BC4B51", size = 5) +
annotate(geom = "text", x = 1959.3, y = -0.5, label = "Decrease",
angle = 90, color = "#5B8E7D", size = 5) +
annotate(geom = "text", x = 2020, y = -0.1, label = "Neutral",
color = "gray", size = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment