You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将marrangeGrob默认按列排列的图表改为按行排列?

如何让marrangeGrob按行排列图表?

嘿,这问题我熟!marrangeGrob默认确实是优先按列来排布图表的,要改成按行排列其实只需要调整一个参数就行。

核心就是把as.table参数设置为TRUE——当这个参数为TRUE时,图表会按照从左到右、从上到下的行优先顺序排列;而默认的FALSE则是列优先(从上到下、从左到右)。

给你修改后的完整代码示例:

library(gridExtra)
library(tidyr)
library(ggplot2)
dat <- c(2,3,4,5,6,7,8,9,0,1)
time <- 1:10
data <- tibble(dat, time)
p1 <- ggplot(data, aes(x= time, y=dat)) + geom_point(color="orange")
p2 <- ggplot(data, aes(x= time, y=dat)) + geom_point(color="blue")
clist <- list(p1,p2)
# 关键修改:as.table=TRUE 开启行优先排列
marrangeGrob(clist, nrow=2, ncol=2, as.table=TRUE )

简单说,nrow和ncol定义了你的布局网格(这里是2行2列),加上as.table=TRUE就会让图表先填满第一行,再填第二行,完美实现你要的按行排列效果~

内容的提问来源于stack exchange,提问作者gaut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:02:45