Ben Ben - 4 months ago 13
R Question

Add annotation outside of stratigraphic plot

How can I add some annotations to a stratigraphic plot?

For example, here's Stratiplot from analogue:

library(analogue)
data(V12.122)
Depths <- as.numeric(rownames(V12.122))
names(V12.122)

(plt <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
data = V12.122,
type = c("h","l","g"),
zones = 400))

plt


enter image description here

I want to add some text in the white space between the blue plots and the zones rectangle on the far right. For example, like this:

enter image description here

With A = 150, B = 600, C = 1000

Answer

Here's one way:

pacman::p_load(analogue)
data(V12.122)
Depths <- as.numeric(rownames(V12.122))
names(V12.122)

(plt <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
                   data = V12.122,  
                   type = c("h","l","g"),
                   zones = 400))

(plt2 <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
                   data = V12.122,  
                   type = c("h","l","g"),
                   yticks = c(150,600,1000)
                   ))

We need to update the y-axis labels like this:

plt2$y.scales$labels <-  c("A", "B", "C")

And then we can plot it with the two y-axes like so:

require(latticeExtra)
doubleYScale(plt,plt2,add.axis=T)

enter image description here

I checked out the source code of this function and figured out that it was a wrapper around xyplot then I did a search to find out that doubleYScale can be used this way to add a 2nd Y axis to an xyscale.

Comments