Skip to content

Problem while collecting axis titles and guides together #359

@Soham6298

Description

@Soham6298

Hello,

I have encountered a problem with patchwork while collecting axis titles and guides together. I think this is the case when individual ggplots have multiple facets. What happens is that I can either collect the axis titles or the guides but not both. This could also happen when legend position is set to bottom. This code snippet should showcase the problem.

library(ggplot2)
library(patchwork)
#testdata
test1 = c(1, 2 ,3, 4)
test2 = c(4, 5, 6, 7) 
type1 = c('A', 'A', 'B', 'B')
type2 = c('C', 'C', 'D', 'D')
df <- data.frame(test1, test2, type1, type2)
df$facet1 <- c('1', '1', '2', '2')
df$facet2 <- c('3', '3', '4', '4')
#plot1
p1 <- ggplot(df, aes(x = test1, y = test2, colour = type1)) +
  theme_bw() +
  geom_point() +
  facet_wrap(~facet1) +
  labs(x = 'numbers', y = 'also numbers', guide = 'letters') +
  scale_colour_manual(values = c('red', 'green', 'blue'))
#plot2
p2 <- ggplot(df, aes(x = test1, y = test2, colour = type2)) +
  theme_bw() +
  geom_point() +
  facet_wrap(~facet2) +
  labs(x = 'numbers', y = 'also numbers', guide = 'letters') +
  scale_colour_manual(values = c('yellow', 'pink', 'orange'))
#combine
p <- (p1 + p2 + plot_layout(axis_titles = 'collect', guides = 'collect') &
        theme(legend.position = 'bottom'))
p

Resulting image:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions