如何部分避免在ggplot中打印? [英] How to partially avoid printing in ggplot?

查看:59
本文介绍了如何部分避免在ggplot中打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下两个功能:

Let's consider these two functions:

function_1 打印一张图

function_1 <- function() {
  print(ggplot() + aes(x=1:10, y = 1:10) + geom_line())
}

function_1()

打印由 function_1

function_2 <- function() {
  patchwork::wrap_plots(function_1(), function_1())
}
function_2()

但是-在这2个并排图之前,我从 function_1 获得了两个图,即通过调用 function_2 获得了三个图:

But - before this 2 side by side plots I get two plots created from function_1 i.e. by calling function_2 I obtain three plots:

  1. function_1
  2. 绘制
  3. function_1
  4. 绘制
  5. 来自 function_1 的两个图的并排图.
  1. Plot from function_1
  2. Plot from function_1
  3. Side by side plot of two plots from function_1.

我的问题是:如何删除前两个图并仅输出第三个图,即 function_2 仅输出并排图,而无需事先打印其他图.我只想重新定义 function_2 (我知道我们只能从 function_1 删除 print()来完成此任务,但是问题已经解决了这是我很大的功能的一部分,我想只考虑 function_2 的更改来解决这个问题.

My question is: How can I delete first two and only output third plot i.e. that function_2 will only output side by side plots without previously printing another plots. I want to do it by only redefining function_2 (I know that we can only delete print() from function_1 and the problem is over, however it's a part of very big function of mine and I would like to deal with this problem with only considering changes in function_2).

问题总结

如何使 function_2 只绘制并排图(这是 function_2 正在制作的三个图中的第三个),而忽略前两个已创建的图?

How to make function_2 to plot only side by side plots (it's the third of three plots that function_2 is making), ignoring first two created plots ?

推荐答案

一种替代方法,可以保留 function_1 的当前行为,但可以解除其武装:

An alternative that preserve's function_1's current behavior but allows a way to disarm it:

function_1 <- function(plot=TRUE) {
  gg <- ggplot() + aes(x=1:10, y = 1:10) + geom_line()
  if (plot) print(gg)
  gg
}
function_2 <- function() {
  patchwork::wrap_plots(function_1(plot=FALSE), function_1(plot=FALSE))
}

这篇关于如何部分避免在ggplot中打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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