I think you are doing this in the wrong order. You need to set the
gpar on the viewport, then compute the grid.rect.
> grid.rect(width=unit(1,'strwidth','Some text'),draw=T,
gp=gpar(font=2))
> grid.text('Some text',y=0.4,gp=gpar(font=2),draw=T)
is one way to do it: pushing a viewport is another.
The factor does depend on the font, family, pointsize, but see e.g.
> strwidth('Some text', units='in')
[1] 0.7503255> strwidth('Some text', units='in', font = 2)
[1] 0.7965495
On Fri, 12 Aug 2011, S?bastien Bihorel wrote:
> Dear R-users,
>
> When one defines a grid unit object using the 'strwidth' dimension,
it
> seems that the default plain font is assumed as the following example
> illustrates. Is there a way to either make use of a font option when
> creating a unit object or to know the factor that exists between the
> width of the same text printed in plain and in bold? This might be
> dependent on the font, though...
>
> require(grid)
>
> grid.rect(width=unit(1,'strwidth','Some text'),draw=T)
>
> grid.text('Some text',draw=T) #
fits
> nicely in the box
> grid.text('Some text',y=0.4,gp=gpar(font=2),draw=T) # partially
outside the box
>
> Thank you in advance for your input on this issue.
>
> Sebastien
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> 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