Skip to content

nrow and ncol are not swapped correctly for vertical facetting #1417

Closed
@chrismerkord

Description

@chrismerkord

There's a bug in facet-wrap.r on lines 89-90 that is not apparent unless you use an nrow other than what it defaults to when nrow is NULL. For example, try:

mpg2 <- subset(mpg, cyl != 5 & drv %in% c("4", "f") & class != "2seater")
base <- ggplot(mpg2, aes(displ, hwy)) + 
  geom_blank() + 
  xlab(NULL) + 
  ylab(NULL)
base + facet_wrap(~class, nrow = 2, dir = "v")

you should get a figure with 2 rows. Instead the figure has 3 rows. Inspecting the plot shows that nrow and ncol both have a value of NULL.

Likewise when you run

base + facet_wrap(~class, ncol = 2, dir = "v")

both nrow and ncol are 2 when it should be nrow == 2 and ncol == NULL.

The problem is that the swap code always results in nrow and ncol being equal or both being NULL, depending on which argument is defined:

# swap
nrow <- sanitise_dim(ncol)
ncol <- sanitise_dim(nrow)  # nrow here is not what you thought it was!

You'll have to create a dummy variable to swap them, e.g.:

# swap
nrow_swap <- ncol
ncol_swap <- nrow
nrow <- sanitise_dim(nrow_swap)
ncol <- sanitise_dim(ncol_swap)

I mentioned this in #1279 but I should have started a new issue instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions