[R] Grid unit width and font face

Paul Murrell p.murrell at auckland.ac.nz
Sun Aug 14 19:39:16 CEST 2011


Hi

On 8/13/2011 9:49 AM, Prof Brian Ripley wrote:
> 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.

And another is to create a text grob and use grobWidth() instead of 
stringWidth() ...

gt <- textGrob("Some text", gp=gpar(cex=2))
grid.draw(gt)
grid.rect(width=grobWidth(gt))

... and another is to provide a name when you draw the text and then 
supply grobWidth() with a gPath rather than a grob ...

grid.text("Some text", gp=gpar(cex=2), name="mylabel")
grid.rect(width=grobWidth("mylabel"))

Paul

> 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.
>>
>
>
>
> ______________________________________________
> 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list