thomasp85 / patchwork

The Composer of ggplots
https://patchwork.data-imaginist.com
Other
2.47k stars 163 forks source link

Enhance patchwork #379

Closed Yunuuuu closed 2 months ago

Yunuuuu commented 2 months ago

I have created some features for patchwork, which will modifying the core codes of patchwork. I hope these features can be merged into patchwork. These functions are now deposited in https://github.com/Yunuuuu/ggalign (See scripts begin with alignpatch-)

  1. free_align: if we want to compose plots without alignment of some panel axes (panel won't be aligned). we can wrap the plot with free_align. Note: this is different from free function (the current free function is much like the wrap_elements() function), it only free the selected axes.
p1 <- ggplot(mtcars) +
    geom_point(aes(mpg, disp)) +
    ggtitle("Plot 1")

p3 <- ggplot(mtcars) +
    geom_point(aes(hp, wt, colour = mpg)) +
    ggtitle("Plot 3")

p5 <- ggplot(mpg, aes(class)) +
    geom_bar() +
    ylim(0, 65) +
    coord_flip()
plot_grid(free_align(p3, "l"), p5, ncol = 1L)

image

  1. free_border: If we want to compose plots without alignment of the panel borders (but still align the panels themselves), we can wrap the plot with free_border. This does the same work with cowplot::plot_grid by setting axis argument, we only align the panel, but border like axis titles won't be aligned. Note title, subtitle, and caption won't be aligned too. If you want to free axes and axes title, try to use free_lab
plot_grid(free_border(p3, "l"), p5, ncol = 1L)

image

  1. free_lab: Similar with free_border, but it will only operate the axis and axis title (labs). If we want to compose plots without alignment of the axis title, we can wrap the plot with free_lab. This should fix https://github.com/thomasp85/patchwork/issues/285, https://github.com/thomasp85/patchwork/issues/348 and https://github.com/thomasp85/patchwork/issues/110#issuecomment-615000426.
plot_grid(free_lab(p3, "l"), p5, ncol = 1L)

image

  1. free_size: Removing the ggplot element sizes when aligning, we ususally want to combine this with free_border to suspend the empty sized ggplot elements. Note the the second plot (on right-top), the left side of the panel won't count space when aligned. This help make more compact layout
    plot_grid(
    patchwork::plot_spacer(), 
    free_size(free_border(p5, "l"), "l"),
    p1, p3, nrow = 2L
    )

    image

Yunuuuu commented 2 months ago
  1. collect guides for every position separately.
    plot_grid(
    p3 + theme(legend.position = "top"), 
    p3 + theme(legend.position = "left"), 
    p3 + theme(legend.position = "bottom"), 
    p3 + theme(legend.position = "right"),
    nrow = 2L,
    guides = TRUE
    )

    image

Yunuuuu commented 2 months ago

If it's okay to merge these features (or some), I would happy to make a commit in the patchwork project.

thomasp85 commented 2 months ago

Thanks - I'm not sure I understand the distinction between free_lab() and free_border() - can you provide an example that shows where it differs?

Yunuuuu commented 2 months ago

Sorry for the missing example. The functions free_lab() and free_border() have a distinction in terms of placement on either the top or bottom side of the panel. Specifically, the top side contains the title and subtitle, while the bottom side contains the caption. free_lab() does not attach these elements in the panel area. Please note the title of the right plot.

p1_top <- ggplot(mtcars) +
    geom_point(aes(mpg, disp)) +
    ggtitle("Plot 1") +
    ggplot2::scale_x_continuous(position = "top")
plot_grid(p1_top, free_lab(p3))

image

plot_grid(p1_top, free_border(p3))

image

thomasp85 commented 2 months ago

Ah, I see... thanks for the examples.

I see at least a place for the functionality of free_align(), free_lab() and free_size() in patchwork. However, I think there is a minimally intrusive way of doing this on top of the free() functionality already provided which I'll try to pursue, so hold off a bit before you spend time porting your approach to patchwork

Yunuuuu commented 2 months ago

Thanks