This vignette covers creating stratigraphic diagrams using ggplot2, highlighting the helpers contained within tidypaleo. The ggplot2 framework was chosen because it is quite flexible and can be used to create almost any time-stratigraphic diagram except ones that involve multiple axes (we can have a fight about whether or not those are appropriate anyway, but if you absolutely need to create them I suggest you look elsewhere). It will help considerably if you are already familiar with ggplot2 (a great tutorial can be found here).
First, you will need to load the packages. For this tutorial, I will use tidypaleo, and the tidyverse (this includes ggplot2). Later, I will use patchwork to create more complex plots. I prefer the
theme_paleo() theme for stratigraphic diagrams using ggplot2, so I will set this here as well. Setting the base font size to a small value (8 pt in this case) is useful for our purposes.
Next, you will need to load the example data. This vignette uses 4 datasets that are already in the correct format for plotting. These are datasets in the tidypaleo package:
halifax_lakes_plottable. These represent four common dataset types for which stratigraphic plots are created.
Geochemical stratigraphic plots include any kind of plot where each panel of the plot should be scaled to the minimum and maximum of the data. This includes (most) non-species abundance data. The example data,
alta_lake_geochem, is a data frame in parameter-long (one row per measurement) form (you may need to use
gather() to get your data into this form, which you can learn about in this tutorial). These measurements are from Alta Lake, British Columbia (Canada), and you can read more about them in the journal article.
alta_lake_geochem #> # A tibble: 192 x 9 #> location param depth age value stdev units n zone #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <int> <chr> #> 1 ALGC2 Cu 0.25 2015. 76 NA ppm 1 Zone 3 #> 2 ALGC2 Cu 0.75 2011. 108. 4.50 ppm 3 Zone 3 #> 3 ALGC2 Cu 1.25 2008. 158 NA ppm 1 Zone 3 #> 4 ALGC2 Cu 1.75 2003. 169 NA ppm 1 Zone 3 #> 5 ALGC2 Cu 2.5 1998. 161 NA ppm 1 Zone 3 #> 6 ALGC2 Cu 3.5 1982. 129 NA ppm 1 Zone 3 #> 7 ALGC2 Cu 4.5 1966. 88.7 3.86 ppm 3 Zone 2 #> 8 ALGC2 Cu 5.5 1947. 65 NA ppm 1 Zone 2 #> 9 ALGC2 Cu 6.5 1922. 62.3 9.53 ppm 3 Zone 2 #> 10 ALGC2 Cu 7.5 1896. 48 NA ppm 1 Zone 2 #> # … with 182 more rows
Plotting a single core with
ggplot() involves several steps:
ggplot()call, where we set which columns will get mapped to the
yvalues for each layer.
geom_point(). We use
geom_lineh()because our plots will be oriented vertically (
geom_line()is designed for horizontal plots).
_gridh()to orient the panels horizontally rather than vertically).
alta_plot <- ggplot(alta_lake_geochem, aes(x = value, y = depth)) + geom_lineh() + geom_point() + scale_y_reverse() + facet_geochem_gridh(vars(param)) + labs(x = NULL, y = "Depth (cm)") alta_plot
That’s it! Of course, there are many modifications that can be made, which are covered in the next few sections.
It is common to highlight certain depths or ages on strat diagrams, or to add things to various places on a plot. This is accomplished by adding additional layers to the plot. For example, highlighting the depth at which the zones change (according to the paper, this is at 4 cm and 16 cm).
geom_hline() function creates its own data using the
yintercept argument, but other geometries require us to create our own data. For example, highlighting the middle zone using
geom_rect() would look like this:
So far, each new layer we have added has been propogated to all facets. To restrict annotations to a specific facet, we need to include the facetting column (in this case,
param) in the layer data. The element copper (Cu) has standards set by the Canadian government (ISQG of 35.7 ppm), and highlighting values that are outside that range can be done in a similar way to highlighting the middle zone.
To highlight the syntax that changed from the original plot, I’ve been using
alta_plot + ..., however with layers such as these, it may be desirable to have the annotation appear under the original data. To do this, you would have to add your annotation layer (
geom_rect()) before your data layer (
alta_lake_geochem data frame, there is a column for standard deviation (
stdev). You can plot these values as error bars using
To control the order of the facets, the column on which they are facetted (
param) must be a
factor(), where the
levels indicate the order. I suggest using
fct_relevel() to do this, so that you only need to specify which items should go first. Usually I do this using a pipe (
mutate() to avoid assigning any intermediate variables.
halifax_geochem data frame was designed so that it is a reasonable number of parameters to plot at one time. Usually you will have a data frame with many more parameters, which you can subset using
filter() before calling
ggplot(). Usually I do this using a pipe (
%>%) to avoid assigning any intermediate variables.
When plotting a single core, it is good practice to plot both ages and depths in the core. In ggplot2, this comes as a second axis generated by a helper function,
scale_y_depth_age(), that uses an
age_depth_model() to produce a second axis. For Alta Lake, the age depth model as published is included in the
alta_lake_bacon_ages object. It’s possible to set the age breaks using the
age_breaks argument, and the labels using the
age_labels argument (this is useful for adding error information to ages on a second axis). The axis title can be set using the
alta_adm <- age_depth_model( alta_lake_bacon_ages, depth = depth_cm, age = 1950 - age_weighted_mean_year_BP ) alta_plot + scale_y_depth_age( alta_adm, age_name = "Age (Year AD)" )
Ideally, we’d like to specify the unit in which each of these parameters was measured. This can be done (1) by renaming each parameter before it gets to
ggplot(), or (2) by using the
units argument of the
facet_geochem_*() functions. I suggest using the second option because it maintains the order of the parameters that you’ve chosen, and keeps the automatic reformatting of
units argument takes a named character vector, which you can create using
c("parameter" = "unit"). If most paremeters are in the same unit, you can pass the default unit using
default_unit = "unit", and if you need to suppress a unit for an item, you can use
alta_plot + facet_geochem_gridh( vars(param), units = c("C/N" = NA, "Cu" = "ppm", "d13C" = "‰", "d15N" = "‰"), default_units = "%" )
So far we’ve plotted one core. That’s great, but often there is more than one core that needs to be plotted. There are two ways to go about this depending on how you would like identical parameters for each lake to be compared: using
facet_geochem_gridh() will stretch the axis limits to fit values from both cores, and creating (and combining) two separate plots using
patchwork::wrap_plots() will result in each panel stretching to the min/max of the data for that parameter. We’ll illustrate both of these using some other data from Long Lake, Nova Scotia.
combined_data <- bind_rows(long_lake_plottable, alta_lake_geochem) combined_data #> # A tibble: 243 x 12 #> dataset location param depth value sd units zone n n_detect age #> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <int> <int> <dbl> #> 1 long_lake… LL PC2 C/N 1 15.4 NA <NA> Unit… 1 1 NA #> 2 long_lake… LL PC2 C/N 6 15.8 0.290 <NA> Unit… 2 2 NA #> 3 long_lake… LL PC2 C/N 11 24.0 NA <NA> Unit… 1 1 NA #> 4 long_lake… LL PC2 C/N 16 21.4 NA <NA> Unit… 1 1 NA #> 5 long_lake… LL PC2 C/N 21 23.2 NA <NA> Unit… 1 1 NA #> 6 long_lake… LL PC2 C/N 26 25.4 NA <NA> Unit… 1 1 NA #> 7 long_lake… LL PC2 C/N 43 27.2 NA <NA> Unit… 1 1 NA #> 8 long_lake… LL PC2 C/N 48 24.3 NA <NA> Unit… 1 1 NA #> 9 long_lake… LL PC2 C/N 53 30.2 NA <NA> Unit… 1 1 NA #> 10 long_lake… LL PC2 C/N 63 24.4 NA <NA> Unit… 1 1 NA #> # … with 233 more rows, and 1 more variable: stdev <dbl>
First we’ll go over the
facet_geochem_gridh() approach, which adds
location as another grouping variable. This will align parameters with themselves vertically, with one row on the plot per core. This usually the best option, as it makes values from one plot to another be directly comparable.
ggplot(combined_data, aes(x = value, y = depth)) + geom_lineh() + geom_point() + scale_y_reverse() + facet_geochem_gridh(vars(param), grouping = vars(location), scales = "free") + labs(x = NULL, y = "Depth (cm)")
The other way to do this is to create two separate plots, and combine them using the patchwork package. This is a good option when values from one core to another are so different that placing them on the same axis results in trends being obscured, or when ages and depths are absolutely necessary for both plots (using
scale_y_depth_age() doesn’t work when combining locations using a facet).
alta_plot_1 <- combined_data %>% filter(location == "ALGC2") %>% ggplot(aes(x = value, y = depth)) + geom_lineh() + geom_point() + scale_y_reverse() + facet_geochem_gridh(vars(param), scales = "free") + labs(x = NULL, y = "Depth (cm)", title = "Alta Lake") long_plot_2 <- combined_data %>% filter(location == "LL PC2") %>% ggplot(aes(x = value, y = depth)) + geom_lineh() + geom_point() + scale_y_reverse() + facet_geochem_gridh(vars(param), scales = "free") + labs(x = NULL, y = "Depth (cm)", title = "Long Lake") library(patchwork) wrap_plots(alta_plot_1, long_plot_2, ncol = 1)
It’s fairly common to see a CONISS dendrogram at the right of plots, and so tidypaleo wouldn’t be complete if it couldn’t make that happen. For a variety of technical reasons, you have to specify that you need
depth mapped to the
y aesthetic for both
layer_dendrogram(). Note that you’ll have to do CONISS using
nested_chclust_coniss(), which uses the rioja package to do the constrained cluster analysis. By default, this will calculate the number of plausible zones based on a broken stick simulation.
coniss <- alta_lake_geochem %>% nested_data(qualifiers = c(age, depth), key = param, value = value, trans = scale) %>% nested_chclust_coniss() alta_plot + layer_dendrogram(coniss, aes(y = depth), param = "CONISS") + layer_zone_boundaries(coniss, aes(y = depth))
If you are familiar with
facet_grid() in ggplot2, you will notice that
facet_geochem_gridh() has done a few things differently, including automatically renaming
d15N to the proper formatting. This function also rotates axis labels, because this is the only sure-fire way to make sure they don’t overlap. You can un-rotate them using
rotate_axis_labels = 0, and you can wrap panels in a defined number of rows or columns using
facet_geochem_wraph(). This works well for plotting a larger number of parameters when there is only one core involved.
Most stratigraphic diagrams are vertical, which is a reflection on the orientation of the core from which the data came. In many cases (especially when age is one of the axes), it may be adventageous to have the plots be horizontal, with time running from left to right along the x-axis. A few things need to change from our original plot, including the mapping, the line geometry, and the facet specification. Here I’ve used
facet_geochem_grid() to stack the plots vertically, but you could also use
ggplot(alta_lake_geochem, aes(x = age, y = value)) + geom_line() + geom_point() + scale_y_reverse() + facet_geochem_grid(vars(param)) + labs(x = "Age (Year AD)", y = NULL)
If our data is relative species abundance, there are a few more restrictions on the plot: each facet must represent values such that 10% abundance on one facet is equal to 10% in another facet, and facet names are often long enough that they must be rotated. The sample data for these exercises is
keji_lakes_plottable, which again is in the form of one row per measurement. This dataset contains two lakes, Beaverskin Lake and Peskawa Lake, both located within Keji National Park, Nova Scotia, Canada (Ginn et al. 2007).
data("keji_lakes_plottable") keji_lakes_plottable #> # A tibble: 202 x 5 #> location depth taxon count rel_abund #> <chr> <dbl> <fct> <dbl> <dbl> #> 1 Beaverskin Lake 0.125 Asterionella ralfsii var. americana (l… 0 0 #> 2 Beaverskin Lake 0.125 Aulacoseira distans 7 3.02 #> 3 Beaverskin Lake 0.125 Aulacoseira lirata 4 1.72 #> 4 Beaverskin Lake 0.125 Cyclotella stelligera 8 3.45 #> 5 Beaverskin Lake 0.125 Tabellaria flocculosa (strain III) 0 0 #> 6 Beaverskin Lake 0.125 Other 213 91.8 #> 7 Beaverskin Lake 0.375 Asterionella ralfsii var. americana (l… 0 0 #> 8 Beaverskin Lake 0.375 Aulacoseira distans 8 3.25 #> 9 Beaverskin Lake 0.375 Aulacoseira lirata 5 2.03 #> 10 Beaverskin Lake 0.375 Cyclotella stelligera 14 5.69 #> # … with 192 more rows
The code to generate the plot is similar to our previous plots, except we use
geom_col_segsh() (which instead of points or a path, draws lines connected to the x-axis), and
facet_abundanceh() (which takes care of rotating facet labels, partially italicizing them, and setting the scales for each panel to start at 0.
keji_plot <- ggplot(keji_lakes_plottable, aes(x = rel_abund, y = depth)) + geom_col_segsh() + scale_y_reverse() + facet_abundanceh(vars(taxon), grouping = vars(location)) + labs(x = "Relative abundance (%)", y = "Depth (cm)") keji_plot
Most of the modifications you can make on these plots are identical to the modifications you can make on non-abundance plots (see previous section), except for a few that we describe below.
There are a few famous styles for abundance plots, because a few existing programs (like Tilia, C2, and rioja) have some very specific defaults that show up on repeat in the literature. These styles can be replicated by changing or adding to
geom_col_segsh(). For example, the classic Tilia “area” style for pollen diagrams:
ggplot(keji_lakes_plottable, aes(x = rel_abund, y = depth)) + geom_areah() + scale_y_reverse() + facet_abundanceh(vars(taxon), grouping = vars(location)) + labs(x = "Relative abundance (%)", y = "Depth (cm)")
ggplot(keji_lakes_plottable, aes(x = rel_abund, y = depth)) + geom_col_segsh() + geom_lineh() + scale_y_reverse() + facet_abundanceh(vars(taxon), grouping = vars(location)) + labs(x = "Relative abundance (%)", y = "Depth (cm)")
It’s common to see some kind of geometry on abundance diagrams exaggerating low-abundance variation. You can use the
*_exaggerate() varieties of
geom_point() to display this information without affecting the scales. If you want to include it only for certain taxa, you can pass
data = keji_lakes_plottable %>% filter(...) to only include certain measurements.
keji_plot + geom_lineh_exaggerate(exaggerate_x = 5, col = "grey70", lty = 2)
To add non-abundance data in the same figure, we need to create two plots and combine them using patchwork’s
wrap_plots() function. In this example, I’m going to use PCA scores, computed by the
nested_prcomp() function, and get them into parameter-long form using
gather(). Note that when I create the plots, I make sure the labels still exist, and then I nix the labels inbetween the two plots using a few
theme() modifications. It’s important to make sure that the panel labels and scale lables are the same before removing them!
keji_pca_scores <- keji_lakes_plottable %>% group_by(location) %>% nested_data(qualifiers = depth, key = taxon, value = rel_abund, trans = sqrt) %>% nested_prcomp() %>% unnest(qualifiers, scores) %>% gather(key = component, value = value, starts_with("PC")) %>% filter(component %in% c("PC1", "PC2")) #> Warning: unnest() has a new interface. See ?unnest for details. #> Try `df %>% unnest(c(qualifiers, scores))`, with `mutate()` if needed keji_pca_plot <- ggplot(keji_pca_scores, aes(x = value, y = depth)) + geom_lineh() + geom_point() + scale_y_reverse() + facet_geochem_gridh(vars(component), grouping = vars(location)) + labs(x = NULL) library(patchwork) wrap_plots( keji_plot + theme(strip.background = element_blank(), strip.text.y = element_blank()), keji_pca_plot + theme(axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) + labs(y = NULL), nrow = 1, widths = c(4, 1) )
Adding a CONISS (or other) dendrogram created using
nested_chclust_coniss() can be done in two ways: if there is already a non-abundance plot that exists, the dendrogram can be added to that plot using
layer_dendrogram(). If there is not already a non-abundance plot, we need to create that plot. Then, the two plots can be combined using patchwork’s
keji_coniss <- keji_lakes_plottable %>% group_by(location) %>% nested_data(qualifiers = depth, key = taxon, value = rel_abund) %>% nested_chclust_coniss() library(patchwork) # method 1: use existing non-abundance plot wrap_plots( keji_plot + theme(strip.background = element_blank(), strip.text.y = element_blank()), keji_pca_plot + layer_dendrogram(keji_coniss, component = "CONISS", aes(y = depth)) + theme(axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) + labs(y = NULL), nrow = 1, widths = c(2, 1) )
# method 2: create a standalone plot for CONISS coniss_plot <- ggplot() + layer_dendrogram(keji_coniss, aes(y = depth)) + scale_y_reverse() + facet_geochem_gridh(vars("CONISS"), grouping = vars(location)) + labs(x = NULL) wrap_plots( keji_plot + theme(strip.background = element_blank(), strip.text.y = element_blank()), coniss_plot + theme(axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) + labs(y = NULL), nrow = 1, widths = c(6, 1) )