Closed MTackenberg closed 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)
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)
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)
Yes, the development version now works for our use case. Thank you!
Super. I'll try to release to CRAN within the next few days.
In R 4.0.2 but not 3.6.3, the use of the arguments
axis
andalign
forplot_grid()
with a legend-only plot produces 'Error: Cannot create zero-length unit vector ("unit" subsetting)'. The error may be related to changes to thegrid
functionunit
in 4.0.0.