Skip to content

X-axis placement with facet_wrap(.., dir = "v", as.table = FALSE) #4553

Closed
@teunbrand

Description

@teunbrand

Hello there,

I was comparing the output of facet_wrap() with an extension thereof I'm trying to make, and I found that there is this one case when the placement of the x-axis is unexpected. In the first row of facets, I would have expected that in the 2nd and 3rd columns of panels, there would be no x-axes, because the bottom row already has these axes.

library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(vars(class), 
             dir = "v", as.table = FALSE)

Created on 2021-07-10 by the reprex package (v1.0.0)

I think I might have narrowed the scope of this glitch down to the lines below, but found it hard to figure out what exactly would need to change. I suspect that in the example above, first_col evaluates to 0, which might violate some assumptions later. Any other combination of the dir and as.table parameters don't have empty panels in the first column, as far as I know.

ggplot2/R/facet-wrap.r

Lines 315 to 341 in acaa98e

if (any(empties)) {
first_row <- which(apply(empties, 1, any))[1] - 1
first_col <- which(apply(empties, 2, any))[1] - 1
row_panels <- which(layout$ROW == first_row & layout$COL > first_col)
row_pos <- convertInd(layout$ROW[row_panels], layout$COL[row_panels], nrow)
row_axes <- axes$x$bottom[layout$SCALE_X[row_panels]]
col_panels <- which(layout$ROW > first_row & layout$COL == first_col)
col_pos <- convertInd(layout$ROW[col_panels], layout$COL[col_panels], nrow)
col_axes <- axes$y$right[layout$SCALE_Y[col_panels]]
inside <- (theme$strip.placement %||% "inside") == "inside"
if (params$strip.position == "bottom" &&
!inside &&
any(!vapply(row_axes, is.zero, logical(1))) &&
!params$free$x) {
warn("Suppressing axis rendering when strip.position = 'bottom' and strip.placement == 'outside'")
} else {
axis_mat_x_bottom[row_pos] <- row_axes
}
if (params$strip.position == "right" &&
!inside &&
any(!vapply(col_axes, is.zero, logical(1))) &&
!params$free$y) {
warn("Suppressing axis rendering when strip.position = 'right' and strip.placement == 'outside'")
} else {
axis_mat_y_right[col_pos] <- col_axes
}
}

I don't think this crucial to fix, as this combination of parameters might not be widely used, but I thought the ggplot2 developers might like to know about this.

Thanks for reading!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorfacets 💎

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions