[R] ggplot facet label font size

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 4 20:33:58 CEST 2006


With ggplot its possible to do this too but in that case it seems
necessary to recurse through the grobs.  Here we look for
grobs that have a label component which contains a colon
and grid.edit those changing the value of cex.  Note that
getNames() give a single grob named "pretty" and we start
from that:

# run code
library(ggplot)
library(grid)

p <- ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill))
gghistogram(p)

recurse <- function(x) {
   if (!is.null(x$label) && regexpr(":", x$label) > 0)
      grid.edit(x$name, gp = gpar(cex = 0.7))
   for (ch in x$children) recurse(ch)
}
recurse(grid.get("pretty"))


On 8/3/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> If you are willing to use grid then you could create only the sex
> factor in the left strips since its already in the desired position
> but when displaying it output a factor.level, i.e. label of "A".
> (my.strip.left is modified from the prior post to do that.)
>
> Then after the plot is drawn, looping through all grobs looking for
> those with a label component of "A" producing a list of grob names,
> strip.left.names.  We then mapply the real factor levels with
> those grobs editing them in reset.levels(), defined below.
> (I have used the fact, empirically determined that the stripts
> are produced in order of the factor levels.)
>
> Everything is the same as the last post except my.strip.left
> which has been modified and everything which comes after the
> call to histogram.
>
> Although this seems to work, maybe Deepayan or Paul can think of
> something slicker.
>
>
> library(ggplot) # data resides here
> library(lattice)
> library(grid)
>
> my.strip <- function(which.given, which.panel, ...)
>   if (which.given == 1 && which.panel[2] == 2)
>      strip.default(which.given, which.panel, ...)
>
> my.strip.left <- function(which.given, which.panel, ...,
>   factor.levels, horizontal)
>   if (which.given == 1 && which.panel[1] == 1)
>      strip.default(which.given, which.panel, factor.levels = LETTERS,
>      horizontal = FALSE, ...)
>
>
> histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip,
>     strip.left = my.strip.left, par.settings = list(add.text =
> list(cex = 0.7)))
>
> is.strip.left <- function(name) identical(grid.get(name)$label, "A")
> strip.left.names <- getNames()[sapply(getNames(), is.strip.left)]
>
> reset.levels <- function(nam, lev) grid.edit(nam, label = lev)
> mapply(reset.levels , strip.left.names, levels(tips$smoker))
>
>
>
>
>
>
> On 8/3/06, Walker, Sam <s-walker at ti.com> wrote:
> >
> > This works OK, but there is some extra spacing between the panels, the
> > top axis and the strip on the top, and the left labels and panel.
> >
> > How can I remove these extra spaces?
> >
> > I've tried changing various layout.widths settings with no luck.  It
> > seems the spaces are calculated based on the number of conditioning
> > variables, in this case 2 (sex+smoker).
> >
> >
> > Thanks in advance...
> > -Sam
> >
> >
> > -----Original Message-----
> > From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
> > Sent: Wednesday, August 02, 2006 6:04 PM
> > To: Walker, Sam
> > Cc: r-help at stat.math.ethz.ch
> > Subject: Re: [R] ggplot facet label font size
> >
> > On 8/2/06, Walker, Sam <s-walker at ti.com> wrote:
> > > How do I change the font size in the facet labels along the edges of
> > the
> > > plot?
> > >
> > > For example (from the ggplot help file):
> > >     p<-ggplot(tips, sex ~ smoker, aesthetics=list(x=tip/total_bill))
> > >     gghistogram(p)
> > >
> > > In this plot, the facet labels are "smoker: No", "smoker: Yes", "sex:
> > > Female", "sex: Male".  What command can I use to reduce the font size
> > of
> > > these labels?
> > >
> > > In lattice terminology, cex is used to scale these strip labels.  But
> > I
> > > couldn't find the equivalent in ggplot.
> > >
> > > The reason I'm asking is I have a 9x7 array of plots which I've been
> > > plotting with lattice.  I wanted to use ggplot because I like having
> > the
> > > labels on the edge of the plots
> >
> > Note that lattice can do that by using custom strip functions:
> >
> > library(ggplot) # data resides here
> > library(lattice)
> >
> > my.strip <- function(which.given, which.panel, ...)
> >   if (which.given == 1 && which.panel[2] == 2)
> >      strip.default(which.given, which.panel, ...)
> >
> > my.strip.left <- function(which.given, which.panel, ..., horizontal)
> >   if (which.given == 2 && which.panel[1] == 1)
> >      strip.default(which.given, which.panel, horizontal = FALSE, ...)
> >
> > histogram(~ tip/total_bill | sex + smoker, tips, strip = my.strip,
> >     strip.left = my.strip.left, par.settings = list(add.text =
> > list(cex = 0.7)))
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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