[R] A problem with plotting a long expression in ylab ?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Sep 28 11:29:14 CEST 2010


On Tue, Sep 28, 2010 at 10:03 AM, Tal Galili <tal.galili at gmail.com> wrote:
> Hello,
>
> It seems that there is a problem when plotting an expression in the ylab of
> a plot in case it is too long.
>
> Example:
>
> plot(1)
> title(ylab = "test
> looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
> ") # work
> plot(1)
> title(ylab = expression(paste("test (% of 360" *degree, ")"))) # works
> plot(1)
> title(ylab = expression(paste("test looooooooooooooooooooooooooong (% of
> 360" *degree, ")"))) # doesn't work

 what does 'work/doesn't work' mean?

 I see some problems with the appearance of the label, and they seem
related to the line breaks in the label. A single line label:

 > plot(1)
 > title(ylab="looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
cat is looooooooooooooooooooooooooooooooooooooooooooooooong")

 (where any line breaks you see are due to mail clients linebreaking)
works fine (except of course the label is truncated at the start and
end).

However if I do:

 > plot(1)
 > title(ylab="looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
cat is\n looooooooooooooooooooooooooooooooooooooooooooooooong")

 (with a newline char \n in there)

 I only see the last "looo*oong" because the first line is out of my
margin area. I can actually see the descender of the 'g', so I know
it's there.

 I think the 'doesn't work' you are having is because there's a
newline on the end of your continuation string:

 > plot(1)
 > title(ylab="foo
+ ")                          # has a newline at end
 > title(ylab="foo")    # doesn't

You can give more label space by using par(mar=, for example:

> par(mar=c(5,8,4,2))
> plot(1)
> title(ylab="long\ncat\nis\nlong")

 But of course you need to know how many lines your ylab will be
before you make the plot...

Barry



More information about the R-help mailing list