披露:我不确定如何为这个问题制作一个可重复的例子.
我正在尝试使用gridExtra包绘制一个grobs列表.
我有一些看起来像这样的代码:
## Make Graphic Objects for Spec and raw traces
for (i in 1:length(morletPlots)){
gridplots_Spec[[i]]=ggplotGrob(morletPlots[[i]])
gridplots_Raw[[i]]=ggplotGrob(rawPlot[[i]])
gridplots_Raw[[i]]$widths=gridplots_Spec[[i]]$widths
}
names(gridplots_Spec)=names(morletPlots)
names(gridplots_Raw)=names(rawPlot)
## Combine spec and Raw traces
g=list()
for (i in 1:length(rawPlot)){
g[[i]]=arrangeGrob(gridplots_Spec[i],gridplots_Raw[i],heights=c(4/5,1/5))
}
numPlots = as.numeric(length(g))
##Plot both
for (i in 1:numPlots){
grid.draw(g[i],ncol=2)
}
Run Code Online (Sandbox Code Playgroud)
让我来看看代码.
morletPlots = ggplots列表
rawplot = ggplots列表
gridplots_spec和gridplots_Raw=上面的ggplots中的grobs列表.
g=上面两个grobs的列表,如此组合gridplots_spec[1],gridplots_raw[1]依此类推等等列表的长度.
现在我的目标是两个将所有这些分为两列.但每当我通过gridplots_spec[i]grid.draw循环时,我都会收到一个错误:
Error in UseMethod("grid.draw") :
no applicable method for 'grid.draw' applied to an object of class "list"
我不能取消它,因为它只是变成一个长字符向量.有任何想法吗?
如果它绝对至关重要,我可以花时间制作一个可重复的例子,但我更可能只是错过了一个简单的步骤.
这是我对您的脚本的解释,如果它不是预期的结果,您可能想要使用一些零碎的部分来使您的问题可重现.
library(grid)
library(gridExtra)
library(ggplot2)
morletPlots <- replicate(5, ggplot(), simplify = FALSE)
rawplot <- replicate(5, ggplot(), simplify = FALSE)
glets <- lapply(morletPlots, ggplotGrob)
graws <- lapply(rawplot, ggplotGrob)
rawlet <- function(raw, let, heights=c(4,1)){
g <- rbind(let, raw)
panels <- g$layout[grepl("panel", g$layout$name), ]
# g$heights <- grid:::unit.list(g$heights) # not needed
g$heights[unique(panels$t)] <- lapply(heights, unit, "null")
g
}
combined <- mapply(rawlet, raw = graws, let=glets, SIMPLIFY = FALSE)
grid.newpage()
grid.arrange(grobs=combined, ncol=2)
Run Code Online (Sandbox Code Playgroud)
编辑我无法抗拒这个恶作剧的黑客为地图着色; 随意忽略它.
palette(RColorBrewer::brewer.pal(8, "Pastel1"))
ggplot.numeric = function(i) ggplot2::ggplot() +
theme(panel.background=element_rect(fill=i))
morletPlots <- lapply(1:5, ggplot)
rawplot <- lapply(1:5, ggplot)
Run Code Online (Sandbox Code Playgroud)