[R] Lattice, ggplot, and pointsize

Milan Bouchet-Valat nalimilan at club.fr
Sat May 25 21:17:18 CEST 2013


Le mardi 21 mai 2013 à 21:39 +0100, Prof Brian Ripley a écrit :
> On 21/05/2013 21:24, Bert Gunter wrote:
> > At the risk of misunderstanding... (inline)
> >
> > On Tue, May 21, 2013 at 12:17 PM, Milan Bouchet-Valat <nalimilan at club.fr> wrote:
> >> Le mardi 21 mai 2013 à 08:17 -0700, Jeff Newmiller a écrit :
> >>> That is like complaining that your hammer does not fit these
> >>> newfangled Philips screws.
> >>>
> >>> These are different tools. Do not expect them to interoperate.
> >> I understand that Lattice and ggplot2 do not use settings from par().
> >> I'm fine with this, as these packages are different from base graphics
> >> and have they own equivalent to tweak settings.
> >>
> >> What I do not understand is that one argument passed to output devices,
> >> which are _not_ provided by package graphics, is ignored by these two
> >> packages. Lattice and ggplot2 do not provide an alternative output
> >> system,
> >
> > False, I believe, depending on what you mean by "output system". They
> > both use grid graphics, not base graphics and with lattice, anyway,
> 
> Indeed.  The issue is a design difference between the base and grid 
> graphics subsystems.  See the 'R Internals' manual for more details.
Thanks for the pointers. Indeed there is some interesting documentation
there. I've also had a deeper look at the code, and I've traced
pointsize (called ps) back to the R_GE_gcontext struct in
src/include/R_ext/GraphicsEngine.h.

If I understand correctly, base graphics draw text using GText() in
src/library/graphics/src/graphics.c, which in turn calls GEText() in
src/main/engine.c. GEText() does take into account the pointsize.

On the other hand, grid graphics draw text using gridText() from
src/library/grid/src/grid.c. This function uses gcontextFromgpar() to
get its R_GE_gcontext object. gcontextFromgpar() (defined in gpar.c)
computes the pointsize from the fontsize gpar setting and a general
scaling of the output:
    /*
     * Scale by GSS_SCALE (a "zoom" factor)
     */
    gc->ps = gpFontSize(gp, i) * REAL(gridStateElement(dd, GSS_SCALE))[0]; 

What is interesting is that when a new device gets initialized by grid
(initGPar() atin gpar.c), the fontsize gpar settings is set to the
device starting pointsize:
    REAL(gpfs)[0] = dev->startps;

And indeed this works with svg():
> svg("test.svg", pointsize=5)
> get.gpar("fontsize")
$fontsize
[1] 5

...but not with Lattice:
> trellis.par.get("fontsize")
$text
[1] 12

$points
[1] 8


So the problem does not appear to be a base vs. grid graphics issue, but
rather something specific to Lattice and ggplot2. And indeed, when
looking at Lattice's sources, canonical.theme() in settings.R does:
             fontsize         = list(text = 12, points = 8),

As simple as that! I didn't need to look so deep into the code... :-/


?trellis.par.set says:
     The initial settings for each device defaults to values
     appropriate for that device. In practice, this boils down to three
     distinct settings, one for screen devices like ‘x11’ and
     ‘windows’, one for black and white plots (mostly useful for
     ‘postscript’) and one for color printers (color ‘postcript’,
     ‘pdf’). [This may not be up-to-date, though...]

So it does not appear completely absurd to try to adjust to the device
settings where appropriate. Pointsize seems such a case to me.
canonical.theme() could set the text font size to get.gpar("fontsize").
Since the default value is 12 for most devices, this would not change
anything by default. (ggplot2 could probably benefit from a similar
change.)

I realize this is no longer a discussion relevant for R as a whole, but
I am posting it here nevertheless in case somebody was interested. Maybe
we should discuss this offlist with Deepayan.


Regards

> > you require a lattice specific device call:
> >
> > ?trellis.device
> > ## this is called automatically when plotting if such a device is not
> > open already.
> >
> > Of course, everything works in R, so if that is what you mean by
> > output system ...
> >
> > Cheers,
> > Bert
> >
> >
> >
> >   so they indeed already "interoperate" with existing output
> >> devices to a certain extent; though they require you to set a separate
> >> option in at least one case. Since package graphics has a way to hook
> >> into the device parameters, maybe Lattice and ggplot2 could have a way
> >> to adapt they default settings to respect them.
> >>
> >>
> >> And BTW, please provide quotations where I am actually complaining. I'm
> >> willing to work on improving things where possible. Please do not
> >> consider any remark or question as a rant -- except if you want to scare
> >> potential contributors away, of course.
> >>
> >>
> >> Regards
> >>
> >>> ---------------------------------------------------------------------------
> >>> Jeff Newmiller                        The     .....       .....  Go Live...
> >>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
> >>>                                        Live:   OO#.. Dead: OO#..  Playing
> >>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> >>> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> >>> ---------------------------------------------------------------------------
> >>> Sent from my phone. Please excuse my brevity.
> >>>
> >>> Milan Bouchet-Valat <nalimilan at club.fr> wrote:
> >>>
> >>>> Le mardi 21 mai 2013 à 23:30 +1000, Duncan Mackay a écrit :
> >>>>> Hi
> >>>>>
> >>>>> See par.settings in xyplot
> >>>>>
> >>>>> Things are also controlled by
> >>>>> trellis.par.get()
> >>>>> to see values
> >>>>> trellis.par.set()
> >>>>>
> >>>>> eg
> >>>>> xyplot(~Freq|Year, data = sheep2,
> >>>>>              groups   = farm,
> >>>>>              par.settings = list(strip.background = list(col =
> >>>> "transparent"),
> >>>>>                                  axis.text = list(cex = 0.75),
> >>>>>                                  par.xlab.text = list(cex = 0.80),
> >>>>>                                  par.ylab.text = list(cex = 0.80)) ,
> >>>> ...)
> >>>>>
> >>>>> HTH
> >>>> Thanks, but that's not really my question. I've already found the way
> >>>> to
> >>>> change text size. What I'm wondering is whether something could be done
> >>>> so that the pointsize argument that is passed to graphical devices has
> >>>> an effect on Lattice and ggplot2 plots.
> >>>>
> >>>>
> >>>> Regards
> >>>>
> >>>>
> >>>>> Duncan
> >>>>>
> >>>>> Duncan Mackay
> >>>>> Department of Agronomy and Soil Science
> >>>>> University of New England
> >>>>> Armidale NSW 2351
> >>>>> Email: home: mackay at northnet.com.au
> >>>>>
> >>>>>
> >>>>>
> >>>>> At 21:18 21/05/2013, you wrote:
> >>>>>> Hi!
> >>>>>>
> >>>>>> When inserting R plots into a document using odfWeave, I fought for
> >>>> a
> >>>>>> while to get Lattice plots use the same text size as base plots. I
> >>>>>> eventually discovered that specifying a point size via e.g.
> >>>>>> svg(pointsize=10) has no effect on Lattice plots. One needs to
> >>>> adjust
> >>>>>> the size manually via:
> >>>>>> trellis.par.set(fontsize=list(text=10, points=8))
> >>>>>>
> >>>>>> This is also developed for both Lattice and ggplot2 by this blog
> >>>> post:
> >>>>>> http://gforge.se/2013/03/exporting-plain-lattice-or-ggplot/
> >>>>>>
> >>>>>> So I am wondering whether is a by-design limitation or whether this
> >>>>>> could be improved. I find it very useful to be able to adapt text
> >>>> size
> >>>>>> to the output device instead of changing plotting parameters for
> >>>> each
> >>>>>> plotting system (especially when you change the resolution of PNG
> >>>>>> output, or move from one output device to another).
> >>>>>>
> >>>>>> Thanks in advance
> >>>>>>
> >>>>>> ______________________________________________
> >>>>>> 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.
> >>>>
> >>>> ______________________________________________
> >>>> 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.
> >
> >
> >
> 
>



More information about the R-help mailing list