Reputation: 7784
I am trying to plot some summary information as text on a plot, using the text
function. What I do not understand is why text
interprets spaces differently than cat
.
Simple exmaple:
spaces <- function(x) paste(rep(" ", x), collapse = "")
vals <- c(1000, 5)
e <- paste0(spaces(3), "val1", spaces(8), "val2\n",
"v: ", vals[1], spaces(12 - nchar(vals[1])), vals[2])
> cat(e) # Gives exactly the output I want
val1 val2
v: 1000 5
plot(0, type = "n", bty = "n", xaxt = "n", yaxt = "n", ylab = "",
xlab = "", xlim = c(0, 5), ylim = c(0, 5))
text(y = 4, x = 1, labels = e, adj = c(0, 1))
As you can see, text
does not handle the spaces the same as the console output. I want things to line up nicely, like they do in the console output. How can I modify the object, or the call to text
so that the plotted version mirrors the console output?
I also tried using:
spaces <- function(x) paste(rep("\t", x), collapse = "")
Upvotes: 1
Views: 75
Reputation: 7784
Based on the very helpful comments from @Jongware, setting par$family
works well for my purposes:
par(family = "mono")
plot(0, type = "n", bty = "n", xaxt = "n", yaxt = "n", ylab = "",
xlab = "", xlim = c(0, 5), ylim = c(0, 5))
text(y = 4, x = 1, labels = e, adj = c(0, 1))
Upvotes: 2