grid.arrange函数调用是否正确?代码报错‘找不到该函数’求助
Your grid.arrange(hist1, hist2, nrow = 2) call is syntactically correct! That’s exactly how you’d arrange two plots (like your histograms hist1 and hist2) into a 2-row vertical layout using this function.
The error you’re seeing has nothing to do with your code’s structure—it means the grid.arrange function isn’t accessible in your current R session. This function isn’t included in base R; it’s part of the gridExtra package. Here’s how to fix this:
- First, install the gridExtra package (you only need to do this once on your system):
install.packages("gridExtra") - Next, load the package into your session every time you start R and want to use
grid.arrange:library(gridExtra) - After loading the package, your original code will run without errors:
grid.arrange(hist1, hist2, nrow = 2)
As a side note, if you’re working with ggplot2 plots, you might also consider the patchwork package for a more intuitive plot-combining syntax, but gridExtra is a reliable choice for this task.
Content of the question originates from Stack Exchange, asked by Fatty Acids.

