Takes a plot or legend and returns a single guide-box in a single column, for embedding in e.g. tables.

guidebox_as_column(legend, which.legend = 1, add.title = FALSE)

Arguments

legend

A ggplot2 plot or the legend extracted with g_legend. Do not provide a ggplotGrob as it is indistinguisble from a legend.

which.legend

Integer, a legend can contain multiple guide-boxes (or vice versa?). Use this argument to select which to use.

add.title

Does nothing yet.

Value

A gtable with keys and labels reordered into a single column and each pair of keys and labels in the same cell.

See also

Examples

library(ggplot2)

p <- ggplot(diamonds, aes(x=x, y=y, colour=cut)) + geom_point()
guidebox_as_column(p)
#> TableGrob (5 x 1) "arrange": 5 grobs
#>   z     cells    name                   grob
#> 1 1 (1-1,1-1) arrange gtable[key-1-1-bg-all]
#> 2 2 (2-2,1-1) arrange gtable[key-3-1-bg-all]
#> 3 3 (3-3,1-1) arrange gtable[key-5-1-bg-all]
#> 4 4 (4-4,1-1) arrange gtable[key-7-1-bg-all]
#> 5 5 (5-5,1-1) arrange gtable[key-9-1-bg-all]
p <- p + guides(colour=guide_legend(ncol=2, byrow=TRUE))
guidebox_as_column(p)
#> TableGrob (5 x 1) "arrange": 5 grobs
#>   z     cells    name                   grob
#> 1 1 (1-1,1-1) arrange gtable[key-1-1-bg-all]
#> 2 2 (2-2,1-1) arrange gtable[key-1-4-bg-all]
#> 3 3 (3-3,1-1) arrange gtable[key-3-1-bg-all]
#> 4 4 (4-4,1-1) arrange gtable[key-3-4-bg-all]
#> 5 5 (5-5,1-1) arrange gtable[key-5-1-bg-all]