Par (mfrow) in R for ggplot

I have this code:

plotfn= function(u) {
  flt = filter(d, utensil ==u)
  ggplot(flt,aes(x=p)) + geom_histogram(binwidth = 0.5, position= position_dodge(0.5), color="black",fill="cadetblue4")+ ggtitle("Histogram of P")+labs( x="P", y="Number of Observations")
}
lapply(unique(d$utensil),plotfn)

      

I tried to do par(mfrow= c(3,3))

to get all 9 graphs in 1 screen, but it doesn't work. I have to use ggplot.

+3


source to share


2 answers


Take a look at the package gridExtra

that works great with ggplot2

and allows you to place multiple graphs on the same page: https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

To use it, store the output of your calls ggplot

to a variable, then pass that variable to grid.arrange

:



myGrobs <- lapply(unique(d$utensil),plotfn)
gridExtra::grid.arrange( grobs = myGrobs, nrow = 3 )

      

+3


source


This should help you:



install.packages("gridExtra")
library(gridExtra)
grid.arrange(plot1, plot2, ..., ncol=3, nrow = 3)

      

0


source







All Articles