Ben Ben - 1 year ago 131
R Question

Use math symbols in panel titles for stratigraphic plot

I want to include math symbols in the panel titles for this stratigraphic plot:

Depths <- as.numeric(rownames(V12.122))

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


enter image description here

For example, I want to have this text in place of "O.univ" etc.:

enter image description here

I used this code to make that text:

plot(1, type="n", axes=FALSE, ann=FALSE)
title(line = -1, main = expression(phantom()^14*C~years~BP))
title(line = -3, main = expression(delta^18*O))
title(line = -5, main = expression(paste("TP ", mu,"g l"^-1)))
title(line = -10, main = expression("very long title \n with \n line breaks"))

But if I try to update the colnames of the data frame passed to
, the code is not parsed, and we do not get the correct text formatting:

V12.122 <- V12.122[, 1:4]
names(V12.122)[1] <- expression(phantom()^14*C~years~BP)
names(V12.122)[2] <- expression(delta^18*O)
names(V12.122)[3] <- expression(paste("TP ", mu,"g l"^-1))

(plt <- Stratiplot(Depths ~ .,
data = V12.122,
type = c("h","l","g"),
zones = 400))


enter image description here

How can I get
to parse the expressions in the colnames and format them correctly in the plot?

I've tried looking through
to see where the panel titles are stored, but no success:

text <- expression(phantom()^14*C~years~BP)
plt$condlevels$ind[1] <- text
names(plt$packet.sizes)[1] <- text
names(plt$par.settings$layout.widths$panel)[1] <- text

Answer Source

You can't actually do this in the current release of analogue; the function is doing too much messing around with data for the expressions to remain unevaluated prior to plotting. I could probably figure this out to allow expressions as the names of the data argument object, but it is easier to just allow users to pass a vector of labels that they want for the variables.

This is now implemented in the development version of the package on github, and I'll push this to CRAN early next week.

This change implements a new argument labelValues which takes a vector of labels for use in labelling the top axis. This can be a vector of expressions.

Here is an illustration of the usage:

df <- setNames(data.frame(matrix(rnorm(200 * 3), ncol = 3)),
               c("d13C", "d15N", "d18O"))
df <- transform(df, Age = 1:200)
exprs <- expression(delta^{13}*C,  # label for 1st variable
                    delta^{15}*N,  # label for 2nd variable
                    delta^{18}*O)  # label for 3rd variable
Stratiplot(Age ~ ., data = df, labelValues = exprs, varTypes = "absolute", type = "h")

which produces

enter image description here

Note that this is just a first pass; I'm pretty sure I haven't accounted for any reordering that goes on with sort and svar etc. if they are used.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download