I'm trying to make my multipanel ggplot
with a shared legend more flexible in a ShinyApp
by allowing the user to choose how many panels to plot.
Currently my code writes out the panel objects 1 at a time like this.
grid_arrange_shared_legend(p1,p2,p3,p4, ncol = 4, nrow = 1)
I do not fully understand why I can not find a way to tell the grid_arrange_shared_legend
to accept a list of plots (list object) rather than writing them out 1 after the other.
It throws this error:
Error in UseMethod("ggplot_build") : no applicable method for 'ggplot_build' applied to an object of class "NULL"
library(ggplot2)
library(lemon)
plotlist <- list()
dsamp <- diamonds[sample(nrow(diamonds), 300), ]
plotlist$p1 <- qplot(carat, price, data = dsamp, colour = clarity)
plotlist$p2 <- qplot(cut, price, data = dsamp, colour = clarity)
plotlist$p3 <- qplot(color, price, data = dsamp, colour = clarity)
plotlist$p4 <- qplot(depth, price, data = dsamp, colour = clarity)
grid_arrange_shared_legend(plotlist, ncol = 4, nrow = 1)
with the use of a list, it would not matter how many plots are in the list, and I would calculate ncol or nrow based on the length of the list...