View source: R/shift_legend_function.R
shift_legend | R Documentation |
This function places the legend of a facetted plot with an uneven number of facets
into the empty space (the empty facet area).
This function is taken from stack overflow: https://stackoverflow.com/questions/54438495/shift-legend-into-empty-facets-of-a-faceted-plot-in-ggplot2
See also https://stackoverflow.com/questions/28496764/add-textbox-to-facet-wrapped-layout-in-ggplot2
and the cowplot package https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
shift_legend(p)
p |
A ggplot object. |
p <- (ggplot2::ggplot(diamonds, aes(x = carat, fill = cut))
+ geom_density(position = "stack") + facet_wrap(~ color) )
library(grid)
grid.draw(shift_legend(p))
p.new <- p +
guides(fill = guide_legend(title.position = "top",
label.position = "bottom",
nrow = 1)) +
theme(legend.direction = "horizontal")
grid.draw(shift_legend(p.new))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.