grantmcdermott · GitHub

This PR introduces a "facet.args" argument that allows users more fine-grained control over facet features. At the moment, only ncol and nrow are supported, but we can obviously add more as the need arises. (E.g. One to allow free axis limits/scales across facets.)

(FWIW @zeileis I tried to go via your "mfrow" argument idea at first. But I quickly ran into trouble because it requires users to specify both the desired no. of rows and columns ahead of time. There's a probably a smart way around this, but I think allowing separate ncol and nrow arguments is a pretty good alternative approach.)

Other features and changes bundled into this PR, addressing some issues from #90.

  • The default facet arrangement is now square (if more than 3 facets are detected).
  • Smarter axis label printing to reduce redundancy (if plot frame is turned off).
  • Legend cex scaled to match the actual size of facet points.
  • Better spacing, especially around the axis titles, between facets, and adjustments in the case of multiline facet titles. I have some other planned improvements for spacing (e.g., around the legend) but will save those for a separate PR since I don't want to confuse where breaking test changes are occurring).

Examples

pkgload::load_all("~/Documents/Projects/plot2")
#> ℹ Loading plot2
palette("dark")
data("penguins", package = "palmerpenguins")
penguins = na.omit(penguins)
with(
  penguins,
  plot2(
    x = flipper_length_mm, y = body_mass_g,
    by = sex,
    facet = species,
    main = "Palmer Penguins",
    frame = FALSE, grid = TRUE
  )
)

# Defaults to square arrangement if no. of facets > 3
with(
  penguins,
  plot2(
    x = flipper_length_mm, y = body_mass_g,
    by = sex,
    facet = interaction(sex, species, sep = "\n"),
    main = "Palmer Penguins",
    frame = FALSE, grid = TRUE
  )
)

# Override default arrangement with facet.args
with(
  penguins,
  plot2(
    x = flipper_length_mm, y = body_mass_g,
    by = sex,
    facet = interaction(sex, species, sep = "\n"),
    facet.args = list(ncol = 2),
    main = "Palmer Penguins",
    frame = FALSE, grid = TRUE
  )
)

Created on 2023-12-09 with reprex v2.0.2

Read the original on github.com ↗