I have a plot, and I'd like to add a text box to it. This text box has text that must go on 5 different lines. This text box also contains superscripts. The text box should be center justified too. I am aware of the posts here and here, but my question is slightly different (please continue reading).
I tried three different solutions (check of the figure below). Each one has its own disadvantages. The first plot (which uses the atop
function) does not result in text that is equally sized on each line because the number of lines (5) is not a power of 2. The second plot (which uses the expression
and paste
functions) is not center justified and has a large gap on the last line. The third plot (which uses multiple text
functions) has the disadvantage of not having proper spacing between each line.
My question is related to the last plot, where the spacing between lines is slightly off. By default, what is the spacing between lines when either atop
or "\n"
are used? I would like to use the third solution I came up with, but I'd like to make sure the spacing is not any different than the default spacing.
layout(matrix(c(1, 1, 2, 2, 0, 3, 3, 0), byrow = T, nrow = 2))
plot(0, type = 'n', main = "Plot 1")
text(1, 0, bquote(atop(atop("Total", "Number of"), atop("Observed", atop("Interactions", "(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))))
plot(0, type = 'n', main = "Plot 2")
text(1, 0, expression(paste("Total\nNumber of\nObserved\nInteractions\n(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))
plot(0, type = 'n', main = "Plot 3")
text(1, 0.4, "Total")
text(1, 0.3, "Number of")
text(1, 0.2, "Observed")
text(1, 0.1, "Interactions")
text(1, 0, expression(paste("(n * m" ^ "-2" * " * wk" ^ "-1" * ")")))
Not sure if this is an appropriate way to do it, but you can find the standard spacing by calling strheight
. This could be simplified, but as a proof of concept it seems to work:
plot(0, type = 'n', main = "Plot 2")
shtxt <- strheight("a")
shgap <- strheight("a\nb") - shtxt * 2
x <- 1
y <- 0
exprs <- list(
expression("Total"),
expression("Number of"),
expression("Observed"),
expression("Interactions"),
expression((n %*% m ^ -2 %*% wk ^ -1))
)
Map(\(e,s) text(x, (shtxt + shgap) * -s + y, e), exprs, seq_along(exprs) - 1)