[R] draw.key; getting grobWidth to respect fontfamily="mono"

Benjamin Tyner btyner at gmail.com
Thu Sep 13 04:33:49 CEST 2012


Many thanks Paul, that is a nifty way to do it.

Regards
Ben

Paul Murrell wrote:
> Hi
>
> On 07/09/12 09:35, Benjamin Tyner wrote:
>> Update: seems one way to skin this cat is to add
>>
>>    gp = gpar(fontfamily="mono")
>>
>> to the viewport() call itself. If anyone has any suggestions for a
>> robust way to extract this piece information from the key grob itself
>> (it's nested several levels deep), I'm all ears.
>
> I'm going to try to blame 'lattice' for this problem (because it 
> appears to be generating "strwidth" units for the text entries in the 
> key rather than "grobwidth" units [so the 'gp' settings on the text 
> grobs are being ignored when widths are calculated]).  (Though 
> 'lattice' is probably doing that to avoid using packGrob() and being 
> further slowed down by 'grid'!)
>
> However, since you are already deep enough into 'grid' to be using 
> viewports and grobWidth() yourself, I suspect that you could get what 
> you want by avoiding draw.key() altogether.  You could generate a text 
> grob yourself (based on your 'GOFlist'), directly query it for its 
> width, build a viewport based on that width (similar to the viewport 
> that you are already creating), push the viewport and draw the grob.
> Something like this ...
>
> GOFgrob <- function(x) {
>     textGrob(c(x$title, x$text$label),
>              x=.5, y=unit(.5, "npc") + unit(1:-1, "lines"),
>              gp=gpar(fontfamily=c("sans", "mono", "mono")))
> }
>
> plot <- xyplot(actual + forecast ~ yyyymm | cond,
>                data = Data,
>                layout = c(1,2),
>                type = "l",
>                panel = function(x,y,GOFlist,...){
>                  panel.xyplot(x,y,...)
>                  pn <- panel.number()
>                  key <- GOFgrob(GOFlist[[pn]])
>                  vp <- viewport(x = 1,
>                                 y = 1,
>                                 width = grobWidth(key) +
>                                         unit(2, "lines"),
>                                 height = grobHeight(key) +
>                                          unit(1, "lines"),
>                                 just = c("right","top")
>                                 )
>                  pushViewport(vp)
>                  grid.draw(key)
>                  grid.rect()
>                  popViewport()
>                },
>                GOFlist = GOFlist
>                )
>
> Hope that helps.
>
> Paul
>
>> Thanks
>> Ben
>>
>> ______________________________________________
>> 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.
>




More information about the R-help mailing list