wilkelab / cowplot

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

Version-specific error in R 4.0.2 when aligning legend-only objects with plot_grid #168

Closed MTackenberg closed 4 years ago

MTackenberg commented 4 years ago

In R 4.0.2 but not 3.6.3, the use of the arguments axis and align for plot_grid() with a legend-only plot produces 'Error: Cannot create zero-length unit vector ("unit" subsetting)'. The error may be related to changes to the grid function unit in 4.0.0.

library('data.table')
library('ggplot2')
library('cowplot')

mainPlot = ggplot(data.table(x = 1, y = 2, z = 'a'),
                  aes(x = x, y = y, color = z)) +
geom_point() + theme(legend.position = 'none')

legend = get_legend(mainPlot + theme(legend.position = 'top'))

gridPlot = plot_grid(mainPlot, legend, axis = 'l', align = 'hv')
clauswilke commented 4 years ago

I can confirm this problem.

library('ggplot2')
library('cowplot')
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

main <- ggplot(
  data.frame(x = 1, y = 2, z = 'a'),
  aes(x = x, y = y, color = z)
) + geom_point() + theme(legend.position = 'none')

legend <- get_legend(main + theme(legend.position = 'top'))

plot_grid(main, legend, align = 'hv')
#> Warning: Graphs cannot be vertically aligned unless the axis parameter is set.
#> Placing graphs unaligned.
#> Warning: Graphs cannot be horizontally aligned unless the axis parameter is set.
#> Placing graphs unaligned.

plot_grid(main, legend, axis = 'l', align = 'hv')
#> Error: Cannot create zero-length unit vector ("unit" subsetting)

Created on 2020-08-28 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

Workaround:

library('ggplot2')
library('cowplot')
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

main <- ggplot(
  data.frame(x = 1, y = 2, z = 'a'),
  aes(x = x, y = y, color = z)
) + geom_point() + theme(legend.position = 'none')

legend <- get_legend(main + theme(legend.position = 'top'))

plot_grid(main, ggdraw(legend), axis = 'l', align = 'hv')

Created on 2020-08-28 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

This should work now in the development version. Could you try for your use case?

library(ggplot2)
library(cowplot)

df <- data.frame(
  short = c("a", "b", "c"),
  long = c("aaaaaaaa", "bbbbbbbb", "ccccccccc"),
  x = 1:3
)

p1 <- ggplot(df, aes(short, x, color = short)) + geom_point()
p2 <- ggplot(df, aes(short, x)) +
  geom_point() + facet_wrap(~long)
p3 <- get_legend(p1)

plot_grid(p1, p2, p3, ncol = 1, axis = "l", align = "v")

plot_grid(p1, NULL, p2, ncol = 1, axis = "l", align = "v")

plot_grid(p1, p2, p3, ncol = 1, axis = "r", align = "v")

plot_grid(p1, NULL, p2, ncol = 1, axis = "r", align = "v")

plot_grid(p1, p2, p3, ncol = 1, axis = "lr", align = "v")

plot_grid(p1, NULL, p2, ncol = 1, axis = "lr", align = "v")

Created on 2020-09-05 by the reprex package (v0.3.0)

MTackenberg commented 4 years ago

Yes, the development version now works for our use case. Thank you!

clauswilke commented 4 years ago

Super. I'll try to release to CRAN within the next few days.