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

geom_tile() not working with facet_wrap() for different ranges of y-axes. #5740

Closed
FloLecorvaisier opened this issue Mar 4, 2024 · 4 comments · Fixed by #5764
Closed

geom_tile() not working with facet_wrap() for different ranges of y-axes. #5740

FloLecorvaisier opened this issue Mar 4, 2024 · 4 comments · Fixed by #5764
Labels
feature a feature request or enhancement layers 📈

Comments

@FloLecorvaisier
Copy link

I found a problem when combining geom_tile() with facet_wrap() when the y-axes of the different facets have fairly different ranges.

I expected that both facets exhibit the same kind of plot, but the one with the greatest range on the y-axis looks odd. Note that creating separate plots (i.e., without using facets but using a subset of the data) make the tiles work for both plots.

Here is the code to reproduce the bug:

library(ggplot2)

data <- data.frame(x = rep(seq(0, 1, length.out = 11), each = (11) * 2),
                   y = c(seq(100, 200, length.out = 11), seq(0, 1, length.out = 11)),
                   z = rnorm(((11) ** 2 * 2)),
                   var = rep(c("T1", "T2"), each = 11))

ggplot(data) +
  geom_tile(aes(x = x, y = y, fill = z)) +
  facet_wrap(vars(var), scales = "free_y")

Created on 2024-03-04 with reprex v2.1.0

@teunbrand
Copy link
Collaborator

The issue here is that the inferred height of the tiles is computed once for the plot as a whole.
You could fix this by setting the appropriate height:

library(ggplot2)

data <- data.frame(x = rep(seq(0, 1, length.out = 11), each = (11) * 2),
                   y = c(seq(100, 200, length.out = 11), seq(0, 1, length.out = 11)),
                   z = rnorm(((11) ** 2 * 2)),
                   height = rep(c(10, 0.1), each = 11),
                   var = rep(c("T1", "T2"), each = 11))

ggplot(data) +
  geom_tile(aes(x = x, y = y, fill = z, height = height)) +
  facet_wrap(vars(var), scales = "free_y")

Created on 2024-03-04 with reprex v2.1.0

@FloLecorvaisier
Copy link
Author

Oh perfect thank you for the tip!

@clauswilke
Copy link
Member

I'm glad there is a workaround to fix the issue, but isn't the default behavior problematic? I would have expected it to work out of the box.

@teunbrand I would vote for keeping this issue open, but don't feel strongly about it. Please do as you see fit. Maybe it'd be so much work to fix that it's not worth it.

@teunbrand
Copy link
Collaborator

It shouldn't be too much of a fuss to correct this. We did it in #5125 for barplots. To solve this problem we'd just have to calculate width as well as height.

@teunbrand teunbrand reopened this Mar 4, 2024
@teunbrand teunbrand added feature a feature request or enhancement layers 📈 labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants