library(ggplot2)
# Basic usage - aligned to panel
ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() +
legend_top()
# Aligned to full plot (useful with titles)
ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() +
labs(title = "My Plot Title") +
legend_top(align_to = "plot")
# Position only the colour legend on top
ggplot(mtcars, aes(mpg, wt, color = factor(cyl), size = hp)) +
geom_point() +
legend_top(by = "colour") +
legend_right(by = "size")
Run the code above in your browser using DataLab