1

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))

<code>text</code> does not handle the spaces correctly.

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 = "")
dayne
  • 7,504
  • 6
  • 38
  • 56
  • 3
    Your image uses a [proportional font](http://en.wikipedia.org/wiki/Proportional_font#Proportional_font); `cat`, when outputting to a typical terminal, does not. – Jongware Apr 17 '14 at 22:11
  • Do you know how to make `text` not use a proportional font? – dayne Apr 17 '14 at 22:15
  • Google turned up an SO answer: http://stackoverflow.com/questions/1395323/fonts-in-r-plots. You'd want to use the generic "mono" or "Courier" (they are probably the same font). – Jongware Apr 17 '14 at 22:25
  • @Jongware If you want to post that as an answer I will happily accept. Thanks for your help!! – dayne Apr 18 '14 at 00:38
  • Nah, go ahead and award them to yourself. All I had was a hunch plus Google. I've never used R myself so this is the very limit of what I *could* do to help. – Jongware Apr 18 '14 at 17:37

1 Answers1

2

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))

enter image description here

dayne
  • 7,504
  • 6
  • 38
  • 56