Re: [R] the title is too long for a graph

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed, 11 Jun 2008 06:57:15 +0100 (BST)

On Tue, 10 Jun 2008, Barry Rowlingson wrote:

> Hua Li wrote:
>> Hi All,
>>
>> I have a problem of putting long titles on a graph:
>>
>> for example,
>>
>> x= seq(1:100)
>> y=seq(1:100)
>>
>> R seems not able to print the whole title. The title content might be
>> changing and thus I don't know how long it is beforehand. Is there a way to
>> measure the length and then put the title into different lines accordingly?
>>
>
> There's a strwidth() function that gives the width of a string on the
> current plot. Note you have to do the plot first so that R knows the
> coordinates of the plot, since strwidth returns the width in plot
> coordinates. So:

As of R 2.7.0, that is only the default. You can have it in inches, for example.

> t="This is a very long title and I would like to split it somewhere, but if I
> split it in the wrong place bad things will happen"
>
> x=(1:10)*100
> y=1:10
> plot(x,y) # x axis is 1:1000
> strwidth(t)
>
> gives:
>
> [1] 2002.939
>
> but
> plot(y,x) # x axis is 1:10
> strwidth(t)
> [1] 20.02939
>
> Now, once you've done your plot you can get the width with:
>
> diff(par()\$usr[1:2])
> [1] 9.72
>
> and then if strwidth if your title is bigger than that you'll need to split
> it into N chunks. Your splitting algorithm will have to be clever if you want
> to break a sentence at word spaces though!
>
> Hopefully this is enough to get you going.
>
> Barry
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

```--
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help