Pollen diagrams in R using rioja - Part 2

In the second part of this guide series, I will show you some additional options and "hacks" that you can use for plotting pollen diagrams using the "rioja" package in R.

Click read more for full details, and a step by step guide.


© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Pollen diagrams using rioja - Part 2

! This guide was updated on 16/02/2018.
Added link to another method to use character strings on the y axis.
! This guide was written using R version 3.4.2 on Windows 10.
And using the "rioja" package, version 0.9-15.1

In the second part of this guide, I show you some additional things you can do to your pollen plots. If you missed part 1, you can find it here. Ideally, you need to have read part 1, or you should at least be aware of how to use rioja in R. This guide follows on directly from part 1, and uses the same dataset, so the code below works if you imported and modified the dataset as per part 1.

The great thing about the strat.plot() function in rioja, is that it offers lots of options to customise your pollen diagrams. But, there's a few things you can't change on the pollen diagram that you might like to. However, there is a way around this, through editing the source code of the strat.plot() function in your R session.

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Change the Y axis labels to characters

Update: For another way to use character strings for the y axis labels that doesn't involve editing the function source code, see part 3 of this guide.

If you remember from part 1, it is not possible to use character strings as the labels for the y axis in the pollen diagram. This is usually not an issue, but if your samples did have character names (such as the surface sample data), then you might want to show this on your plot.

To edit the source code of the function, type fix(strat.plot) into the R console. This will start the R text editor, which should contain the source for the function. You can safely edit the source code, as any changes you make will only apply to the R session you are currently working in. i.e. if you close and then re-open R, the source code will revert to the original code.

You can use find and replace in this editor to easily find the code that we're going to alter. To change the y axis labels, we need to find the following code:

labels = as.character(y.tks) which you should replace with labels = y.override

Save your changes, then exit the code editor.

Now, you need to create the "y.override" object, which will contain your sample names, and then run the plot code:

library(rioja)
# Create y axis names
y.override <- row.names(ma.pollen1)
# Create colour scheme
p.col.group <- c(rep("blue", times=16), rep("red", times=14), rep("green2", times=3))
# Plot
pol.plot <- strat.plot(ma.pollen1, yvar=y.scale, y.tks=y.scale, y.rev=TRUE, plot.line=FALSE, plot.poly=FALSE, plot.bar=TRUE, col.bar=p.col.group, lwd.bar=10, sep.bar=TRUE, scale.percent=TRUE, xSpace=0.01, x.pc.lab=TRUE, x.pc.omit0=TRUE, srt.xlabel=45, las=2)

Which will result in the following plot:

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Add additional axes to the plot

strat.plot() will plot the abundance values along the bottom x axis, and the pollen type (or species) along the top x axis in your diagram. If you also wanted to plot the abundance values along the top x axis, you'll need to edit the source code.

Open the source code editor fix(strat.plot) and search for the following code:

axis(side = 1, at = xlabb, labels = xlabbt, mgp = mgpX, cex.axis = cex.axis, ...)

Leave this code in place and add the following code underneath:

axis(side = 3, at = xlabb, labels = xlabbt, mgp = mgpX, cex.axis = cex.axis, ...)

Next, find the following code:

r <- (usr1[4] - usr1[3]) * 0.01

Leave this code in place, but add +1 after 0.01, so it should now look like this:

r <- (usr1[4] - usr1[3]) * 0.01 +1

Now, save and exit the code editor. Then run the plot code, adding mgp=c(3, -0.25, -1) to your arguments. This will alter the position of the two x axes and labels, moving them closer to the diagram.

# Plot
pol.plot <- strat.plot(ma.pollen1, yvar=y.scale, y.tks=y.scale, y.rev=TRUE, plot.line=FALSE, plot.poly=FALSE, plot.bar=TRUE, col.bar=p.col.group, lwd.bar=10, sep.bar=TRUE, scale.percent=TRUE, xSpace=0.01, x.pc.lab=TRUE, x.pc.omit0=TRUE, srt.xlabel=45, las=2, mgp=c(3, -0.25, -1))

Which results in the following plot:

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

More control over X axis labels

strat.plot() gives you pretty good control over the x axis labels already, allowing you to change the tick increments, removing the "0" value label, or only showing the min and max labels.

If you wanted more control, for example, showing ticks every 5%, but only showing the labels for every other tick (e.g. 5, 15, 25 etc.), you'll need to edit the source code.

Open the source code editor and find the following code:

xlabbt <- as.character(xlabb)

Leave this code in place, and add the following code underneath:

my.xlabbt <- replace(xlabbt, grep("0", xlabbt), NA)

Then find labels = xlabbt and replace with labels = my.xlabbt (There should be two to replace, if you added the top x axis)

Next, run the plot code ensuring it includes x.pc.inc=5 in the arguments.

# Plot
pol.plot <- strat.plot(ma.pollen1, yvar=y.scale, y.tks=y.scale, y.rev=TRUE, plot.line=FALSE, plot.poly=FALSE, plot.bar=TRUE, col.bar=p.col.group, lwd.bar=10, sep.bar=TRUE, scale.percent=TRUE, xSpace=0.01, x.pc.inc=5, x.pc.lab=TRUE, x.pc.omit0=TRUE, srt.xlabel=45, las=2, mgp=c(3, -0.25, -1))

Which will result in the following plot: (look closely at the x axis labels to see the changes).

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Remove the black lines from the diagram

Surprisingly, strat.plot() does not include an option to remove the black lines that are plotted in the pollen diagrams. If you want to remove these lines, or change their appearance, you'll need to edit the source code.

Open the source code editor and find the following code:

lines(c(0, 0), c(min(yvar, na.rm = TRUE), max(yvar, na.rm = TRUE)), ...) (note, this code is split over two lines in the editor, try finding: lines(c(0, 0), if you get an error)

If you want to remove the lines, then simply delete the code. If you want to change their appearance (e.g. colour, line type etc.), then add the additional variables to the code. Save the code, and exit the editor.

Now, assuming you have made all the changes to the source code listed in this guide, and you were to now plot a pollen diagram (bar plot with cluster analysis), it would look something like this:

Which looks pretty awesome! Thanks for reading! and please leave any comments or questions below.


Ad

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Further reading

Pollen diagrams using rioja - Part 1 - In the first part of this guide, I show you how to plot pollen diagrams using rioja.

Pollen diagrams using rioja - Part 3 - In the third part of this guide, I show you how to use multiple plot types on the same pollen diagram.


No comments:

Post a Comment