Closed Soham6298 closed 6 months ago
When individual ggplots have two or more facets, collecting axis titles and guides together seems to struggle. Especially when legend position is at the bottom. Here's an example code that would showcase the issue.
` library(ggplot2) library(patchwork)
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')
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'))
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')) p <- (p1 + p2 + plot_layout(axis_titles = 'collect', guides = 'collect') & theme(legend.position = 'bottom')) p When removingguides = 'collect'`, the axis titles are collected though.
When removing
When individual ggplots have two or more facets, collecting axis titles and guides together seems to struggle. Especially when legend position is at the bottom. Here's an example code that would showcase the issue.
` 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')) p <- (p1 + p2 + plot_layout(axis_titles = 'collect', guides = 'collect') & theme(legend.position = 'bottom')) p
When removing
guides = 'collect'`, the axis titles are collected though.