As of October 2019 this method doesn’t work any longer, due to an update in the vctrs package. https://github.com/r-spatial/sf/issues/1172. So your best bet is to have the same structure in your shapefile and use :
We can then create a 3 rows data frame containing a list-column in which we store the sf object. Then we just unnest it. This operation erases the sf-class, we have to add it back.
Before posting we have to create an authorization token once.
# Registration
# run this part once and write down client_id and client_secret,
# you can then comment this part
r <- POST(paste0(instance , "api/v1/apps"),
body = list(client_name = "my_application_name",
redirect_uris = "urn:ietf:wg:oauth:2.0:oob",
scopes = "write"))
stop_for_status(r)
apps <- content(r)
paste("client_id :", apps[["client_id"]])
paste("client_secret", apps[["client_secret"]])
# end of registration ; set your client id/secret below
The use case is to create the same plot in different languages. I used this technique for Wikipediaplots.
We are going to build a list containing all translations, we will then loop over each language, generating and saving the plot.
# Mauna Loa atmospheric CO2 change
# multi language plot for Wikipedia
# Required packages
library(tidyverse)
library(gridExtra)
library(scales)
library(lubridate)
library(Hmisc)
# Translations ------------------------------------------------------------
language <- list(
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
co2ml <- read_csv("http://scrippsco2.ucsd.edu/assets/data/atmospheric/stations/in_situ_co2/monthly/monthly_in_situ_co2_mlo.csv",
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)
current <- language[[l]]
# format the date in local names
Sys.setlocale("LC_TIME", current$locale_lc_time)
# main plot
p1 <- ggplot(co2ml, aes(decimal, co2_filled)) +
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
p2 <- ggplot(co2ml, aes(vdate, delta)) +
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
p1 + annotation_custom(grob = ggplotGrob(p2), xmin = 1957, xmax = 1991, ymin = 361, ymax = 412)
ggsave(file = paste("co2_mauna_loa", l, Sys.Date(), "wp.svg", sep = "_"), width = 20, height = 20, units = "cm", device = svg)
}
A tutorial to compute trends by groups and plot/map the results
We will use dplyr::nest to create a list-column and will apply a model (with purrr::map) to each row, then we will extract each slope and its p value with map and broom::tidy.
A tutorial to use your Zotero references with {rmarkdown} : easily add the citations, automatically generate your bibliography, including the references of the packages used in your analysis.
It’s a good practice to cite the R packages you use in your analysis. However it can be cumbersome to maintain the list of your package’s references in Zotero while the packages used can change when you update your script. Here we use the automatic updates of Zotero to generate our main bibliography and we auto-generate the package bibliography and then merge them.
To easily add citations in the rmarkdown text in RStudio, we can also add the {citr} package.
Build the Zotero library
Add references to Zotero.
Create a collection for your project and drag and drop the references needed in this collection.
Zotero
Export the collection (BibLatex format). Tick the update option.
Export
Save it in your R project folder as zotero.bib. Each time you modify this Zotero collection, the zotero.bib file will be updated. Check in the Zotero settings :
In the setup chunk, we load the required packages, generate the .bib file for the packages, merge it with our Zotero bibliography and add the packages as nocite (they are not cited in the text but have to appear in the references).
We must run the setup chunk at least once to generate the files and check which citation to use for the packages that provide multiple references.
We can then carry on our analysis with R chunks and some text with citations.
The citations can be added with [@key] where key is the citation key generated by Better Bibtex and found at the top of the Zotero info panel. Or we can use {citr} which will help us find it (Addins menu in Rstudio > Insert citations).
The bibliography will be added at the end of the document.
---
title: "Bibliography with knitr : cite your references and packages"
author: "r.iresmi.net"
date: "`r Sys.Date()`"
output: html_document
bibliography: biblio.bib
csl: iso690-author-date-fr-no-abstract.csl
link-citations: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# all your necessary packages
packages <- c("tidyverse",
"knitr",
"bibtex"
# add your other packages here
)
# install if needed and load packages
to_install <- packages[! packages %in% installed.packages()[, "Package"]]
if (length(to_install)) {
install.packages(to_install, repos = "https://cloud.r-project.org")
}
invisible(lapply(packages, library, character.only = TRUE))
# get the packages version
packages_versions <- function(p) {
paste(packageDescription(p)$Package, packageDescription(p)$Version, sep = " ")
}
# Get the packages references
write.bib(packages, "packages.bib")
# merge the Zotero references and the packages references
cat(paste("% Automatically generated", Sys.time()), "\n% DO NOT EDIT",
{ readLines("zotero.bib") %>%
paste(collapse = "\n") },
{ readLines("packages.bib") %>%
paste(collapse = "\n") },
file = "biblio.bib",
sep = "\n")
# Some packages reference keys must be modified
# (their key is not the package name)
# check in packages.bib
packages_keys <- packages %>%
enframe() %>%
mutate(value = case_when(value == "knitr" ~ "@knitr1",
#value == "boot" ~ "@boot1",
TRUE ~ paste0("@", value)))
```
---
nocite: |
@Svenssonguideornitho2007
`r paste(packages_keys$value, collapse = "\n ")`
---
Any text...
Data analyzed with R [@r_development_core_team_r:_2010][^r_version].
[^r_version]: `r version$version.string`, with : `r paste(lapply(sort(packages), packages_versions), collapse = ", ")`.
We can easily cite references (menu Addins / Insert citations) after having run the setup chunk to create the bibliography file [@moreno_measuring_2017].
Lorem Ipsum
## References