我有一个想要传递给patchwork::wrap_plots的地块列表:
library(tidyverse)
library(patchwork)
# plots with na
dummy_plot <- ggplot() + theme_void()
p <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
plots <- list(p, p, NA)
patchwork::wrap_plots(plots)给出错误:
错误:只知道如何添加ggplots和/或grobs
因此,我想对我的列表进行预处理,用上面空白的虚拟图替换任何NA。
预期结果:
plots <- list(p, p, NA)
... some magic r code here to replace NA with dummy plot ...
# now plots looks like this
plots <- list(p, p, dummy_plot)现在,我可以传递到没有错误的情况下包装情节:

如何将列表中NA的实例替换为我的虚拟情节?
发布于 2021-04-14 20:31:58
您可以遍历plots并检查元素是否为identical到NA。如果是,请用dummy_plot替换
plots[sapply(plots, function(x) identical(x, NA))] <- list(dummy_plot)
patchwork::wrap_plots(plots)
# grid.rect(gp = gpar(fill = NA))结果

https://stackoverflow.com/questions/67098268
复制相似问题