Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
teunbrand opened this issue Jul 10, 2021 · 2 comments · Fixed by #4673
Closed

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

teunbrand opened this issue Jul 10, 2021 · 2 comments · Fixed by #4673
Labels
bug an unexpected problem or unintended behavior facets 💎

Comments

@teunbrand
Copy link
Collaborator

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!

@thomasp85
Copy link
Member

Great sleuthing - Will take a look

@teunbrand
Copy link
Collaborator Author

Thank you for your work Thomas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior facets 💎
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants