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

Disregard axis labels when positioning legend boxes #333

Open
stitam opened this issue Sep 19, 2023 · 4 comments
Open

Disregard axis labels when positioning legend boxes #333

stitam opened this issue Sep 19, 2023 · 4 comments

Comments

@stitam
Copy link

stitam commented Sep 19, 2023

Hi,

Maybe not an issue, but I couldn't get this to work: FigA has very long y axis labels at the right side of the plot. FigB has a legend box also in the right side of the plot. When I compose FigA/FigB, the legend box for FigB is placed very far from the plot because it only starts where the y axis labels for FigA end. What I want is to keep the plot areas aligned but disregard the space required for y axis labels when calculation the position of the legend box. Any way to do this? Many thanks.

@stitam
Copy link
Author

stitam commented Sep 27, 2023

I have another question related to this. Is it possible to tell patchwork which plot goes where using plot_layout(design = c(area(),area(), etc.)), but then do not do any other aligning? I would like to replicate a programmable version of creating a number of plots and just putting them next to each other on a canvas. The only extra I want is guide_area() for the combined legend, but I do not want patchwork to align the plot areas or the text labels. Thanks.

@thomasp85
Copy link
Owner

For the first question the answer is no, you can't both get aligned plot regions and disregarding all other alignment.

For the second question I'm afraid I don't really understand what you are asking - can you provide some code and visual for it?

@stitam
Copy link
Author

stitam commented Oct 30, 2023

Thanks @thomasp85 here is an example, for both really:

library(ggplot2)
library(patchwork)
rm(list = ls())

# import mtcars
data(mtcars)

# prep a long column
mtcars <- tibble::rownames_to_column(mtcars, "model") |>
  dplyr::mutate(model = paste(model, model, model, sep = "_"))

mtcars$cyl <- as.character(mtcars$cyl)
mtcars$gear <- as.character(mtcars$gear)

g1 <- ggplot(mtcars, aes(model, mpg)) + geom_col(aes(fill = cyl)) + coord_flip()
g2 <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(col = gear)) + coord_flip()

g <- g2 + g1 + guide_area() + plot_layout(
  design = c(
    area(1, 1, 45, 100),
    area(46, 1, 90, 100),
    area(91, 1, 100, 100)
  ),
  guides = "collect"
) &  theme(
  legend.position = "bottom",
)

mtcars

I made the model names super long to illustrate my problem: patchwork aligns the plot panels i.e. the grey plot areas and also the axis labels which is generally nice, but in this particular case the y axis label on the top plot will be very far from the plot panel!

I would like to be able to tell patchwork that "here is area(1,1,45,100) dedicated for the top figure, put the figure in that area but do not align the panel with any of the adjacent plots". Ideally this would create a composite plot where the y axis labels are aligned as before but the panel for the top plot is much wider than the panel for the bottom plot.

I hope this clarifies my issue? Many thanks.

(Also note in this particular case the guide area is not in the middle of the third row because it is aligned to the panel and not the overall width of the plot).

@thomasp85
Copy link
Owner

I just added a free() function I think might help you. Please let me know if this is what you are after

free(g2) + free(g1) + guide_area() + plot_layout(
    design = c(
        area(1, 1, 45, 100),
        area(46, 1, 90, 100),
        area(91, 1, 100, 100)
    ),
    guides = "collect"
) &  theme(
    legend.position = "bottom",
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants