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) library(GSoCggduoTest) 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.