-
Notifications
You must be signed in to change notification settings - Fork 2.1k
aspect ratio of plot not working with of space = "free" in facet_grid #3834
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
Comments
library(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) +
geom_point(aes()) +
facet_grid(vars(cyl), scales = "free", space = "free")
# works
p # fails properly
p + coord_fixed()
#> Error: coord_fixed doesn't support free scales
# do not fail, but the plot is strange
p + theme(aspect.ratio = 1) Created on 2020-02-29 by the reprex package (v0.3.0) |
Thanks for the input, I think your example more correctly describes the problem! |
Ah bummer. This change breaks a perfectly fine plot. :( MWE below... If I want to make a gridded heatmap plot with geom_tile(), y is a factor, and x is character there is really no inherent scale to either axis. However, the only way to guarantee that my output plot has square tiles, while dropping irrelevant categories, is to use Example:
|
@baderstine I'm surprised that this ever worked. It's unclear in the general case what exactly the output should be. You can always make individual plots and stitch them together with patchwork. Shouldn't be that much more work. See below for a rough example. You'd have to put in a little more work to remove the y axis labels from the interior plots, but that's maybe 5 additional lines of code or so. library(tidyverse)
library(patchwork)
y <- factor(x=c(1:6), labels=letters[1:6])
x <- c("cat","dog","house","farm","road","tree")
xgrp <- c("animal","animal","building","building","infrastructure","nature")
data <- tibble(y, x, xgrp)
make_plot <- function(data) {
ggplot(data, aes(y = y, x = x)) +
geom_tile(width = .8, height = .8) +
scale_y_discrete(limits = letters[1:6]) +
coord_fixed() +
facet_grid(cols = vars(xgrp, x))
}
data %>%
mutate(
group = interaction(x, xgrp)
) %>%
nest(data = -group) %>%
mutate(
plots = map(data, ~make_plot(.x))
) %>%
pull(plots) %>%
wrap_plots(nrow = 1) Created on 2021-10-21 by the reprex package (v2.0.0) |
Interesting... so something like the following does the trick (although this is digging into a separate patchwork issue thomasp85/patchwork#150)
|
I was just wondering is it possible to have this properly fixed? |
as in, restore the previous functionality when the x and y axes are discrete or what? |
Yes, please! |
@baderstine @Gsmith535 |
Yep it's a bummer that a plot is possible to render but instead we get an error message. |
I'm getting to feel this might sound right, though I'm still in confusion what's the appropriate behavior... Anyway, it's not a good idea to keep discussing on a closed issue. Could you file a new issue describing what's the problem and what would be the "fix", with a minimal reprex? The rendered version of #3834 (comment) at y <- factor(x = c(1:6), labels = letters[1:6])
x <- c("cat", "dog", "house", "farm", "road", "tree")
xgrp <- c("animal", "animal", "building", "building", "infrastructure", "nature")
xval <- rnorm(6)
dat <- data.frame(y, x, xgrp, stringsAsFactors = F)
devtools::load_all("~/GitHub/ggplot2/")
#> ℹ Loading ggplot2
# this plot has the correct aspect ratio, but it includes every single x category in every facet, regardless of whether a value is actually present:
ggplot(dat, aes(y = y, x = x)) +
geom_tile(width = .8, height = .8) +
theme(aspect.ratio = 1) +
facet_grid(cols = vars(xgrp, x), drop = T) # , scales="free_x", space="free_x") # this plot drops the irrelevant x categories in each facet, however my tiles are no longer guaranteed to be square:
ggplot(dat, aes(y = y, x = x)) +
geom_tile(width = .8, height = .8) +
# theme(aspect.ratio = 1) +
facet_grid(cols = vars(xgrp, x), drop = T, scales = "free_x", space = "free_x") # this plot is perfect (in a previous version of ggplot2), but now I'm not allowed to make it:
ggplot(dat, aes(y = y, x = x)) +
geom_tile(width = .8, height = .8) +
theme(aspect.ratio = 1) +
facet_grid(cols = vars(xgrp, x), drop = T, scales = "free_x", space = "free_x") Created on 2022-06-24 by the reprex package (v2.0.1) |
@baderstine @yutannihilation I was just wondering why is this issue closed? Is the proper thing to reopen this issue or to open a new issue. Thank you. |
I think the real issue is that we (maybe just me) have confused library(tidyverse)
df <- tibble(
x = 1:3,
y = 2*(1:3),
z = 3*(1:3)
)
ggplot(df, aes(x, y)) +
geom_point() +
theme(aspect.ratio = 1) ggplot(df, aes(x, y)) +
geom_point() +
coord_fixed() Created on 2022-06-24 by the reprex package (v2.0.1) Allowing The new issue should focus on this point specifically. |
In my understanding, |
This comment was marked as resolved.
This comment was marked as resolved.
@yutannihilation I think that's the request, yes, and it would be consistent with what And then this should also be properly documented, because right now if you read the documentation for |
@yutannihilation better version of the reprex:
|
@clauswilke library(ggplot2)
ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
theme(aspect.ratio = 1) +
facet_grid(vars(cyl), scales = "free") Created on 2022-06-25 by the reprex package (v2.0.1) @baderstine |
@yutannihilation Wait, does @baderstine just have to remove |
@baderstine library(ggplot2)
y <- factor(x = c(1:6), labels = letters[1:6])
x <- c("cat", "dog", "house", "farm", "road", "tree")
xgrp <- c("animal", "animal", "building", "building", "infrastructure", "nature")
xval <- rnorm(6)
dat <- data.frame(y, x, xgrp, stringsAsFactors = F)
my.aspect.ratio <- length(unique(dat$y))
ggplot(dat, aes(y = y, x = x)) +
geom_tile(width = .8, height = .8) +
theme(aspect.ratio = my.aspect.ratio) +
facet_grid(cols = vars(xgrp, x), drop = T, scales = "free_x") Created on 2022-06-25 by the reprex package (v2.0.1) |
I think he wants the individual facets square rather than the overall plot. |
Yep, it's about the shape of the individual plotted elements within each facet, not about the shape of the overall plot. |
I guess i don't have a good enough reprex to demonstrate the issue. If I remove the space = "free_x" then individual facets that contain multiple x values are squished. I'll work on a new example. |
Thanks, now I understand your intention, but I don't feel @clauswilke |
Re-reading the documentation of everything, I think We also have to accept that sometimes we have to carefully calculate the width and height of a plot such that individual facets come out just right, and this can't always be the job of ggplot2, sometimes it's on the user. That's what I did for example to make sure that all the individual squares came out right in this chapter of my book: https://clauswilke.com/dataviz/directory-of-visualizations.html |
Hmm. thanks. I don't agree with the idea of the overall aspect ratio of the plot at the moment, but it might be just that I don't read the documentation enough. As it turned out we all have different opinions on this topic, probably a closed issue is too small for the discussion. But let me clarify before filing a new issue. Do you mean both of the following results are wrong as library(ggplot2)
d <- data.frame(
x = c(1, 2, 3, 1, 1, 2),
y = c(1, 2, 3, 2, 3, 2),
g = rep(c("a", "b", "c"), times = c(3, 1, 2))
)
p <- ggplot(d, aes(x, y)) +
geom_point() +
theme(aspect.ratio = 1)
p + facet_wrap(vars(g)) p + facet_grid(vars(g)) Created on 2022-06-25 by the reprex package (v2.0.1) |
Oh, sorry, maybe I misunderstood and was wrong about the aspect ratio. Are you saying it currently applies consistently to individual facets? Maybe that's fine then, except I don't know what it would/should do when the axis ranges are different in different facets. |
Yes, I believe that's the current behavior.
In my understanding, as you commented on #3834 (comment), the aspect ratio is about the frame of the plot/panel and irrelevant to the actual values inside. So, I think the same rule applies to |
Ok, that seems reasonable to me, but it would probably not produce the plot @baderstine was hoping for. |
Thanks. Yeah, let's think about what option is needed to make it possible again next... |
I was about to file a new issue, but it seems #4584 is the one. |
Is there any way to get the same plot as plot4 in the new version of ggplot2 (e.g., 3.4.0)? Thanks! |
have you tried ggforce ? |
I believe the |
Uh oh!
There was an error while loading. Please reload this page.
Hi, in ggplot I want to set the aspect ratio of the plot to a certain number.
space = "free"
infacet_grid
does not seem to work withtheme(aspect.ratio = 1)
. Is there a workaround for this. Also I usecoord_flip
which hinders the use ofcoord_fixed.
This is what I get:The text was updated successfully, but these errors were encountered: