wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Issue horizontal alignment when strip.placement set to 'outside' in ggplot2 #106

Closed lcougnaud closed 5 years ago

lcougnaud commented 6 years ago

I experienced an issue when aligning two ggplot2 plots: one facetted plot and one without facetted. The two plots are not properly aligned:

library(ggplot2)
data(iris)
plot1 <- ggplot(data = iris) + geom_point(aes(x = `Sepal.Length`, y = `Sepal.Width`)) + 
    facet_grid(Species~., switch = "y") + theme(strip.placement = "outside")
plot2 <- ggplot(data = iris) + geom_point(aes(x = `Petal.Length`, y = `Petal.Width`, col = "Species"))
plot_grid(plot1, plot2, ncol = 1, align = "v", axis = "lr")

plot_gridwithwstripplacement I narrowed down the issue to the use of the strip placement, placed outside of the plot. If the placement remains inside the plot, the plots are properly aligned according to the x-axis:

library(ggplot2)
data(iris)
plot1 <- ggplot(data = iris) + geom_point(aes(x = `Sepal.Length`, y = `Sepal.Width`)) + 
    facet_grid(Species~., switch = "y")
plot2 <- ggplot(data = iris) + geom_point(aes(x = `Petal.Length`, y = `Petal.Width`, col = "Species"))
plot_grid(plot1, plot2, ncol = 1, align = "v", axis = "lr")

plot_gridwithoutwstripplacement The issue seems fixed when the 'complex alignments' (vcomplex_align, hcomplex_align set to TRUE) are used within the align_plots function.

Session information: R version 3.3.3 (2017-03-06)

Platform: x86_64-pc-linux-gnu (64-bit)

Running under: Ubuntu 16.04.4 LTS

clauswilke commented 5 years ago

This works now with the development version of cowplot. I hope to release relatively soon.

library(ggplot2)
library(cowplot)
#> 
#> 
#> *******************************************************
#> Note: cowplot does not change the default ggplot2 theme
#> anymore. To recover the previous behavior, execute:
#>   theme_set(theme_cowplot())
#> *******************************************************
data(iris)
plot1 <- ggplot(data = iris) + geom_point(aes(x = `Sepal.Length`, y = `Sepal.Width`)) + 
  facet_grid(Species~., switch = "y") + theme(strip.placement = "outside")
plot2 <- ggplot(data = iris) + geom_point(aes(x = `Petal.Length`, y = `Petal.Width`, col = "Species"))
plot_grid(plot1, plot2, ncol = 1, align = "v", axis = "lr")

Created on 2019-05-11 by the reprex package (v0.2.1)