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

add option align_axis_title #373

Closed
wants to merge 10 commits into from
Closed

Conversation

Yunuuuu
Copy link
Contributor

@Yunuuuu Yunuuuu commented Aug 2, 2024

fix #285, #348 and #110 (comment)

This PR provide the option to disable the alignment of the axis title.

library(tidyverse)
library(patchwork)

df1 <- data.frame(Group = factor(1:10), Value = runif(10))
df2 <- data.frame(Group = factor(1:10), Long_label_A = runif(10), Long_label_B = runif(10), Long_label_C = runif(10)) %>%
  gather(Label, X, -Group)

p1 <- ggplot(df1, aes(Value, Group)) +
  geom_col(orientation = "y", position = "identity")

p2 <- ggplot(df2, aes(Label, Group, fill = X)) +
  geom_tile() +
  ylab("") +
  theme_void() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

p1 + p2 + plot_layout(
      nrow = 1, widths = c(0.9, 0.1),
      align_axis_title = FALSE
    )

image

Created on 2024-08-02 with reprex v2.1.0
~
~

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 2, 2024

Another example in #348

library(ggplot2)
library(tibble)
library(patchwork)

df <- mtcars |>
  rownames_to_column("car") |>
  as_tibble()

gg1 <- df |>
  ggplot(aes(y = wt, x = car)) +
  geom_col() +
  theme_bw() +
  xlab(NULL) +
  ylab("Plot 1") +
  theme(axis.text.x = element_blank())


gg2 <- df |>
  ggplot(aes(y = "number_of_cylinders", x = car, fill = as.character(cyl))) +
  geom_tile(width = 0.9) +
  ylab("Plot 2") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
gg1 / gg2 + plot_layout(align_axis_title = FALSE)

Created on 2024-08-03 with reprex v2.1.0
~
~
~
~
~
image

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 2, 2024

Example for comment #110 (comment)

library(patchwork)
library(ggplot2)

p1 <- ggplot(diamonds, aes(clarity, price)) +
  geom_boxplot() +
  coord_flip()

lev <- levels(diamonds$cut)
long_label <- paste("Very Very Very Very", lev)
names(long_label) <- lev
p2 <- ggplot(diamonds, aes(cut, price)) +
  geom_boxplot() +
  scale_x_discrete(labels = long_label) +
  coord_flip()
p1 + p2 + plot_layout(ncol = 1, align_axis_title = FALSE)

Created on 2024-08-03 with reprex v2.1.0
~

image

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 2, 2024

Another example for facet panels:

library(patchwork)
library(ggplot2)

p <- ggplot(mpg, aes(displ, cty)) +
    geom_point()
p1 <- p + facet_grid(rows = vars(drv))
p2 <- p + facet_grid(vars(drv), vars(cyl))
p3 <- p + facet_wrap(vars(class))
p1 + (p2 + scale_y_continuous(labels = ~ paste0("very very long ", .x))) +
    plot_layout(align_axis_title = FALSE, ncol = 1L)

image

p3 + (p2 + scale_y_continuous(labels = ~ paste0("very very long ", .x))) +
    plot_layout(align_axis_title = FALSE, ncol = 1L)

image

Created on 2024-08-03 with reprex v2.1.0
~
~
~
~

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 5, 2024

Dear @thomasp85,

I apologize for any inconvenience caused and kindly request your assistance in integrating this function into my package. The functionality of my package heavily depends on this particular function.

I have developed a package that utilizes ggplot2 to generate complex heatmaps. The package functions smoothly, but it heavily relies on this specific function. Without integrating this function, there would be some areas with no data. If it is not possible to merge it in patchwork, I will continue with other tasks in my package.

Some figures created with this package.
ggalign (2)

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 25, 2024

After learning patchwork project, I found a better method to implement this, including a series of functions:

  • free_align()
  • free_border()
  • free_lab()
  • free_size

These function need lots of modifications of patchwork, I'll make another commits for this. Now I'll close this thread.

@Yunuuuu Yunuuuu closed this Aug 25, 2024
@thomasp85
Copy link
Owner

Hi @Yunuuuu - thanks for your wish to improve upon patchwork. Before you spend too much time on new features it would be better to open an issue where your ideas can be discussed. In that way we can ensure that your time is best spend on work that will end up in the package

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Aug 26, 2024

Thanks @thomasp85 for your advice.

I have made an issue to follow this

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

Successfully merging this pull request may close these issues.

Gap between axis label and text if adjacent plot has vertical axis text
2 participants