# Mauna Loa atmospheric CO2 change
# multi language plot for Wikipedia
# Required packages
library(tidyverse)
library(gridExtra)
library(scales)
library(lubridate)
library(Hmisc)
# Translations ------------------------------------------------------------
<- list(
language en_US = list(
locale_lc_time = "en_US.UTF-8",
title = expression(paste("Monthly mean ", CO[2], " concentration ")),
caption = paste("Data : R. F. Keeling, S. J. Walker, S. C. Piper and A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Accessed ", Sys.Date()),
x = "Year",
y = expression(paste(CO[2], " fraction in dry air (", mu, "mol/mol)")),
x2 = "Month",
y2 = expression(atop(paste(CO[2], " fraction in dry air (", mu, "mol/mol)"), "Departure from yearly average")),
title2 = "Seasonal variation"
),fr_FR = list(
locale_lc_time = "fr_FR.UTF-8",
title = expression(paste("Moyenne mensuelle de la concentration de ", CO[2])),
caption = paste("données : R. F. Keeling, S. J. Walker, S. C. Piper et A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Accédé le", Sys.Date()),
x = "année",
y = expression(paste("fraction de ", CO[2], " dans l'air sec (", mu, "mol/mol)")),
x2 = "mois",
y2 = expression(atop(paste("fraction de ", CO[2], " dans l'air sec (", mu, "mol/mol)"), "en écart à la moyenne annuelle")),
title2 = "Variation saisonnière"
),de_DE = list(
locale_lc_time = "de_DE.UTF-8",
title = expression(paste("Monatliche durchschnittliche ", CO[2], "-Konzentration")),
caption = paste("Datei : R. F. Keeling, S. J. Walker, S. C. Piper und A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Zugänglich am", Sys.Date()),
x = "Jahr",
y = expression(paste(CO[2], "-Anteil in trockener Luft (", mu, "mol/mol)")),
x2 = "Monate",
y2 = expression(atop(paste(CO[2], "-Anteil in trockener Luft (", mu, "mol/mol)"), "Abweichung vom Jahresmittel")),
title2 = "Monatliche Variation"
),es_ES = list(
locale_lc_time = "es_ES.UTF-8",
title = expression(paste("Media mensual de la concentración de ", CO[2])),
caption = paste("dato : R. F. Keeling, S. J. Walker, S. C. Piper y A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Visitada", Sys.Date()),
x = "Año",
y = expression(paste("Fraccion de ", CO[2], " en aire secco (", mu, "mol/mol)")),
x2 = "Mes",
y2 = expression(atop(paste("Fraccion de ", CO[2], " en aire secco (", mu, "mol/mol)"), "Desviación de la media anual")),
title2 = "Variación mensual"
),cs_CZ = list(
locale_lc_time = "cs_CZ.UTF-8",
title = expression(paste("Průměrné měsíční koncentrace oxidu uhličitého")),
caption = paste("data : R. F. Keeling, S. J. Walker, S. C. Piper a A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Přístupné", Sys.Date()),
x = "rok",
y = expression(paste("koncentrace ", CO[2], " v suchém vzduchu (", mu, "mol/mol)")),
x2 = "měsíc",
y2 = expression(atop(paste("koncentrace ", CO[2], " v suchém vzduchu (", mu, "mol/mol)"), "odchylka od ročního průměru")),
title2 = "Měsíční změna (průměrná roční odchylka)"
),nn_NO = list(
locale_lc_time = "nn_NO.UTF-8",
title = expression(paste("Gjennomsnittlig månedlig ", CO[2], "-konsentrasjon")),
caption = paste("data : R. F. Keeling, S. J. Walker, S. C. Piper og A. F. Bollenbacher\nScripps CO2 Program (http://scrippsco2.ucsd.edu). Vist", Sys.Date()),
x = "År",
y = expression(paste(CO[2],"-andel i tørr luft (", mu, "mol/mol)")),
x2 = "Måned",
y2 = expression(atop(paste(CO[2],"-andel i tørr luft (", mu, "mol/mol)"),
"Avvik fra årlig gjennomsnitt")),
title2 = "Årlig variasjon"
)
)
# Data --------------------------------------------------------------------
# http://scrippsco2.ucsd.edu/data/atmospheric_co2/primary_mlo_co2_record
# used during US gov shutdown
<- read_csv("http://scrippsco2.ucsd.edu/assets/data/atmospheric/stations/in_situ_co2/monthly/monthly_in_situ_co2_mlo.csv",
co2ml col_names = c("year", "month", "xls_date", "decimal",
"co2", "co2_seas_adj", "fit", "fit_seas_adj",
"co2_filled", "co2_filled_seas_adj"),
col_types = "iiiddddddd",
skip = 57,
na = "-99.99",
comment = "\"") %>%
group_by(year) %>%
mutate(year_mean = mean(co2_filled, na.rm = TRUE),
delta = co2_filled - year_mean,
vdate = ymd(paste0("2015-", month, "-01")))
# Generate the plot for each language -------------------------------------
for (l in names(language)) {
message(l)
<- language[[l]]
current
# format the date in local names
Sys.setlocale("LC_TIME", current$locale_lc_time)
# main plot
<- ggplot(co2ml, aes(decimal, co2_filled)) +
p1 geom_line(color = "pink") +
geom_point(color = "red", size = 0.6) +
stat_smooth(span = 0.1) +
scale_x_continuous(breaks = pretty_breaks()) +
scale_y_continuous(breaks = pretty_breaks(4), minor_breaks = pretty_breaks(8)) +
labs(
x = current$x,
y = current$y,
title = current$title,
subtitle = paste("Mauna Loa", min(co2ml$year), "-", max(co2ml$year)),
caption = current$caption) +
theme_bw() +
theme(plot.caption = element_text(size = 7))
# inset plot
<- ggplot(co2ml, aes(vdate, delta)) +
p2 geom_hline(yintercept = 0) +
stat_smooth(span = 0.4, se = FALSE) +
stat_summary(fun.data = "mean_cl_boot", colour = "red", size = 0.3) +
scale_x_date(breaks = pretty_breaks(4), minor_breaks = pretty_breaks(12), labels = date_format("%b")) +
labs(
x = current$x2,
y = current$y2,
title = current$title2) +
theme_bw()
# merge the plots and export in SVG
+ annotation_custom(grob = ggplotGrob(p2), xmin = 1957, xmax = 1991, ymin = 361, ymax = 412)
p1 ggsave(file = paste("co2_mauna_loa", l, Sys.Date(), "wp.svg", sep = "_"), width = 20, height = 20, units = "cm", device = svg)
}
The use case is to create the same plot in different languages. I used this technique for Wikipedia plots.
We are going to build a list containing all translations, we will then loop over each language, generating and saving the plot.
Last version of the code on Gitlab.