Hard: Make a function that prints the legend of a ggplot2 object. Display the results in a knitr/Rmarkdown document.
This function builds the ggplot2 object, then filters out the legend. Then it returns the gtable object containing the legend. When it is ready to printed, the s3 print method will be invoked
Example:
library(ggplot2)
## [1] TRUE
library(GSoCggduoTest)
## [1] TRUE
histPlot <- qplot(x = Sepal.Length, data = iris, fill = Species, geom = "histogram", binwidth = 1/4)
(right <- histPlot)
(bottom <- histPlot + theme(legend.position = "bottom"))
(top <- histPlot + theme(legend.position = "top"))
(left <- histPlot + theme(legend.position = "left"))
grab_legend(right)
grab_legend(bottom)
grab_legend(top)
grab_legend(left)