 GeoEki -4 years ago 157
R Question

# Line break in R plot legend

I create many plots in R with data input from another script that are held in a separate variable each. I put the variables in a string and force a line break with

`\n`
. This works as intended, but the legend is not justified at all.
`xjust`
and
`yjust`
seem to not do anything. Also, when placing the legend e.g. in bottomright, it stretches beyond the margin of the plot. Any idea how I can properly place my legend justified at the corner of a plot?

Here a reproducible code snippet:

``````plot(c(0,3), c(0,3), type="n", xlab="x", ylab="y")

a <- 2.3456
b <- 3.4567
c <- 4.5678
d <- 5.6789

Corner_text <- function(text, location = "bottomright"){
legend(location, legend = text, bty = "o", pch = NA, cex = 0.5, xjust = 0)
}
Corner_text(sprintf("a = %3.2f m\n b = %3.2f N/m\UB2\n c = %3.2f deg\n d = %3.2f perc", a, b, c, d))
`````` Cath

`legend` is usually used to explain what the `points` or `lines` (and the different colors) represent. Therefore, inside the legend box (`bty`) there is a space where the lines/points are supposed to be. This probably explains why you think your text is not left-justified (you also have a problem of space after your line-break (`\n`): if you put a space after a line-break, it will be your first character on the next line, hence the text does not appear justified).

In your example, you don't have lines or points to explain, hence, I would use `text` rather than `legend`.
To know where "bottomright" is on your axes, you can use the graphical parameters `par("xaxp")` and `par("yaxp")` (it gives you the values of first and last ticks and the number of ticks on your axis). On the x-axis, from the last tick, you need to shift left to have space for the widest line.

In `R code`, it gives:

``````# your plot
plot(c(0,3), c(0,3), type="n", xlab="x", ylab="y")

# your string (without the extra spaces)
text_to_put <- sprintf("a = %3.2f m\nb = %3.2f N/m\UB2\nc = %3.2f deg\nd = %3.2f perc", a, b, c, d)

# the width of widest line
max_str <- max(strwidth(strsplit(text_to_put, "\n")[]))

# put the text 