[R] Plot alignment with mtext

Sarah Goslee sarah.goslee at gmail.com
Tue Nov 15 20:52:54 CET 2011


Hi Kevin,

On Tue, Nov 15, 2011 at 2:36 PM, Kevin Burton <rkevinburton at charter.net> wrote:
> I would like the text plotted with 'mtext' to be alighned like it is for
> printing on the console. Here is what I have:

You don't provide any of the info in the posting guide (OS may be
important here), or a reproducible example, which would also be
helpful.

But see below anyway.

>
>
>> print(emt)
>
>                    ME                   RMSE                  MAE
> MPE           MAPE         MASE
>
> original -1.034568e+07 1.097695e+08 2.433160e+07 -31.30554   37.47713
> 1.5100050
>
> xreg        1.561235e+01 2.008599e+03 9.089473e+02 267.05490 280.66734
> 0.9893643
>
>
>
>> a <- capture.output(print(emt))
>
>> a
>
> [1] "                    ME         RMSE          MAE       MPE      MAPE
> MASE"
>
> [2] "original -1.034568e+07 1.097695e+08 2.433160e+07 -31.30554  37.47713
> 1.5100050"
>
> [3] "xreg      1.561235e+01 2.008599e+03 9.089473e+02 267.05490 280.66734
> 0.9893643"
>
>
>
> There are no tabs but when adding to a plot with mtext like:
>
>
>
> op <- par(mfcol = c(2, 1), oma=c(0,0,4,0))
>
> . . . . .
>
> a <- capture.output(print(emt))
>
> mtext(a[1], line= 1, side=3, outer=TRUE)
>
> mtext(a[2], line= 0, side=3, outer=TRUE)
>
> mtext(a[3], line=-1, side=3, outer=TRUE)
>
>
> The plotted text is not aligned like when it is displayed on the console. I
> have looked at the strings and they all have the same length so it seems
> that mtext is doing something with the spaces so that the output is not
> aligned. Any ideas on how I can get it aligned (by column)?

The default font used for titles is not proportionally spaced, at
least on my linux
system, so of course they won't line up.

Try:

a <- c("                    ME         RMSE          MAE       MPE
 MAPE      MASE",
"original -1.034568e+07 1.097695e+08 2.433160e+07 -31.30554  37.47713
1.5100050",
"xreg      1.561235e+01 2.008599e+03 9.089473e+02 267.05490 280.66734
0.9893643")

par(mfcol=c(2,1), oma=c(0,0,4,0))
plot(1:10, 1:10)
plot(1:10, 1:10)

par(family="mono")
mtext(a[1], line= 1, side=3, outer=TRUE)
mtext(a[2], line= 0, side=3, outer=TRUE)
mtext(a[3], line=-1, side=3, outer=TRUE)

Or whatever the appropriate font family specification for your OS is.

Sarah



-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list