R中的par(mfrow)对于ggplot [英] par(mfrow) in R for ggplot

查看:104
本文介绍了R中的par(mfrow)对于ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

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)

我尝试执行 par(mfrow = c(3,3))在1个屏幕中获取所有9个图,但这是行不通的.我必须使用ggplot.

I tried doing a par(mfrow= c(3,3)) to get all 9 plots in 1 screen but it doesn't work. I have to use ggplot.

推荐答案

看看 gridExtra 软件包,该软件包与 ggplot2 很好地集成在一起,并允许您放置多个绘制到单个页面上: https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

Take a look at the gridExtra package, which integrates nicely with ggplot2 and allows you to place multiple plots onto a single page: https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

要使用它,请将您的 ggplot 调用的输出存储到一个变量,然后将该变量传递给 grid.arrange :

To use it, store the output of your ggplot calls to a variable, then pass that variable to grid.arrange:

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

这篇关于R中的par(mfrow)对于ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆