-
Notifications
You must be signed in to change notification settings - Fork 181
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
facet_grid (geom_tile) with different y-axis label order per facet. #245
Comments
Can you create a reprex (a minimal reproducible example) demonstrating what you are trying to do? The goal of a reprex is to make it easier to recreate your problem so that other people can understand it and/or fix it. If you've never heard of a reprex before, you may want to start with the tidyverse.org help page, and you may want to look at the similar questions folks have asked in juliasilge/juliasilge.com#10. Thanks! 🙌 |
Hello @juliasilge, Thanks a lot for your attention. Here, I believe, is a reproducible example:
So, you can see that now
However, as you can see below the order is always the same in both facets: Kind regards, George. |
That example is a little complicated. Can you take a look at this and explain what it is you are wanting to do? library(tidyverse)
df <- data.frame(
col1 = factor(sample(c("a", "b", "c"), 20, replace = TRUE)),
col2 = factor(sample(c("x", "y", "z"), 20, replace = TRUE)),
col3 = rnorm(20),
col4 = sample(c("grid_a", "grid_b"), 20, replace = TRUE),
col5 = sample(c("grid_c", "grid_d"), 20, replace = TRUE)
)
df |>
ggplot(aes(col1, col2)) +
facet_grid(col4 ~ col5, scales = "free", space = "free") +
geom_tile(aes(fill = col3)) Created on 2025-01-15 with reprex v2.1.1 Are you wanting to order |
Hello @juliasilge, Apologies for my confusing example and thanks for providing a much simpler one. Continuing with your example... I would like to reorder the permutations (col1 and col2) within col4, for example. Thus, the y-axis order could be say (z, y, x) in grid_a while being (y, z, x) in grid_b. I would like to be able to plot the different grids with specific y-axis orders based on the same data frame. Is my intention clearer now? Kind regards, George. |
That definitely works! You can do that like so: library(tidyverse)
library(tidytext)
df <- data.frame(
col1 = factor(sample(c("a", "b", "c"), 20, replace = TRUE)),
col2 = factor(sample(c("x", "y", "z"), 20, replace = TRUE)),
col3 = rnorm(20),
col4 = sample(c("grid_a", "grid_b"), 20, replace = TRUE),
col5 = sample(c("grid_c", "grid_d"), 20, replace = TRUE)
)
df |>
mutate(col2 = reorder_within(col2, col3, col4)) |>
ggplot(aes(col1, col2)) +
facet_grid(col4 ~ col5, scales = "free", space = "free") +
geom_tile(aes(fill = col3)) +
scale_y_reordered() Created on 2025-01-17 with reprex v2.1.1 |
Hello @juliasilge, Thanks a lot. I can indeed replicate your code. However, when I try to adapt to mine it does not work. Thus, I believe I am doing something wrong. Would you be so kind as to tell me what that would be? Kind regards, George.
|
I think you'll need to not pass in a separate |
Hello.
Thanks a lot for the useful tool!
I would like to ask if it would be possible to use
tidytext
to generate a facet_grid (geom_tile) where the order of the y-axis label would vary on each facet. It seems thatggplot
does allow for this behaviour, but I was wondering if this would be possible with the help oftidytext
.Many thanks in advance, George.
The text was updated successfully, but these errors were encountered: