Inserting units in expressions for labels
Jun 11, 2016
I show how to construct an expression for e.g. an axis label, if the unit is stored in a variable.
Preface. This is my shortest blog item ever, showing in a few lines something that took me over an hour to realize: use [[1]]
in refering expressions stored in variables.
Introduction
I wanted this for oce
, which for issue 981 required a method of extracting units from objects, and inserting the values into expressions being constructed for the mtext
calls that write axis labels.
Methods
The code says it all: use [[1]]
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
par(mar=rep(0, 4))
plot(0:5, 0:5, xlab="", ylab="", axes=FALSE, type='n')
## Simple method
text(1, 1, expression("A ["*m/s^2*"]"))
## Unit stored in an expression: naive approach.
u <- expression(m/s^2)
text(1, 2, bquote("B ["*.(u)*"]"))
## Unit stored in an expression: note the [[1]] selection!
text(1, 3, bquote("C ["*.(u[[1]])*"]"))
## Unit stored in an oce-formatted list
U <- list(unit=expression(m/s^2), scale="")
text(1, 4, bquote("D ["*.(U[[1]][[1]])*"]"))
Resources
- Jekyll source code for this blog entry: 2016-06-11-flags.Rmd