IrisPlots <- iris %>% ## Creates a list of bar plots, one for each species.
group_by(Species) %>%
do(plots = ggplot(data = .) +
aes(x = Sepal.Length) +
geom_bar() +
labs(title = .$Species))
IrisPlots$plots[1] ## Prints the first plot from the list
class(IrisPlots$plots[1]) ## shows the class of the object
So here is where I found the problem. The do() process stripped the "gg" and "ggplot" class from each of the objects within the list. This isn't an issue if I wanted to print each one individually, but if I want to combine them (I do), ggarrange() can't handle the "list" objects.
SingleIrisPlot <- ggplot(data = iris,
aes(x = Sepal.Length, y = Sepal.Width, colour = factor(Species))) +
geom_point()
SingleIrisPlot
class(SingleIrisPlot)
This creates a second plot to join with ggarrange.
ggarrange(IrisPlots$plots[1],SingleIrisPlot)
This throws an error. Is there a way to reattach the class of "gg" or "ggplot" or a way to restructure the do() call?