zeileis · GitHub

In #558 we discussed the idea that facet = y ~ 1 could be used as a shortcut for facet = ~ y, facet.args = list(nrow = length(unique(y))). Analogously, facet = 1 ~ x could be used as a shortcut for facet = ~ x, facet.args = list(nrow = 1).

I have implemented this now by tweaking tinyframe() slightly. This now distinguishes ~ x from 1 ~ x by returning NULL for the yfacet in the former case but a zero-column data frame in the latter case. In other words, a zero-column data frame signals: This part of the formula had a specification but no variables in it. In tinyplot.formula() I've extended the processing of xfacet and yfacet correspondingly.

In principle, this works but there are two caveats:

  1. tinyplot.default() handles the case with facet.args slightly differently from facet attributes.
  2. The processing has only been extended in the formula method. I just noticed now that the is an additional function get_facet_fml() (https://github.com/grantmcdermott/tinyplot/blob/facet-formula/R/facet.R#L613-L658) that does something similar to my tinyformula()/tinyframe() processing but is only used in the default method. We should probably try to use the same code in both cases.

To illustrate the first point, consider

library("tinyplot")
tinytheme("clean2")
p = transform(penguins, group = factor(paste(species, island, sep = "-")))
tinyplot(bill_dep ~ bill_len, data = p, facet = 1 ~ group)                            ## top
tinyplot(bill_dep ~ bill_len, data = p, facet = ~ group, facet.args = list(nrow = 1)) ## bottom

facet2

tinyplot(bill_dep ~ bill_len, data = p, facet = group ~ 1)                            ## left
tinyplot(bill_dep ~ bill_len, data = p, facet = ~ group, facet.args = list(nrow = 5)) ## right

facet1

Note that in the formula-based specification we get the right facet strips as well. Probably this should only be done in case both the number of rows and the number of columns is greater than 1? I wasn't sure though where to best add this information.

Read the original on github.com ↗