简体   繁体   English

如何在 R 中使用函数获得的多个图创建演示文稿?

[英]How to create a presentation in R with several plots obtained by a function?

I have created one function that gives you the number of plots that you want, depending on a list.我创建了一个函数,根据列表为您提供所需的绘图数量。

This is the example that I have created.这是我创建的示例。

list_genes <- c("GEN1", "GEN2", "GEN3")


myfunction <- function(x,y){
  
  for(gene in list_genes){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      stripchart(x, method="jitter", vertical=F, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
  }
}

myfunction(x=c(1,5,6,2,4,30,23,12,45))

图片1

My final objective is to create a presentation (automatically) with all of the plots that I generate from my function.我的最终目标是使用我从函数生成的所有图创建一个演示文稿(自动)。

However, If I try to create an ioslides_presentation , they don't appear (only the first one and a bit of the second).但是,如果我尝试创建一个ioslides_presentation ,它们不会出现(只有第一个和第二个的一点)。

---
title: "My presentation"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## PLOTS 

```{r}
list_genes <- c("GEN1", "GEN2", "GEN3")


myfunction <- function(x,y){
  
  for(gene in list_genes){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      stripchart(x, method="jitter", vertical=F, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
  }
}

myfunction(x=c(1,5,6,2,4,30,23,12,45))
```

图像2

The code is a small example of my original function but it is enough to show you my problem.该代码是我原始函数的一个小例子,但足以向您展示我的问题。 I tried to save the plots into objects but since it is base R, I can't (or at least, it doesn't work properly).我试图将绘图保存到对象中,但由于它是基础 R,我不能(或者至少,它不能正常工作)。 I know that I could do this with ggplot2 but, I wanted to ask here just in case someone knows how to do make presentations in this case, before changing the complete original function.我知道我可以用ggplot2做到这一点,但是,我想在这里问一下,以防万一有人知道如何在这种情况下进行演示,然后再更改完整的原始功能。

Does anyone know how to solve it?有谁知道如何解决它?

Thanks very much in advance首先十分感谢

Regards问候

There are several ways to get this.有几种方法可以得到这个。 The, IMHO easiest is to take the mfrow or mfcol call out of the function and to create a global one for all plots, eg:恕我直言,最简单的方法是从函数中取出mfrowmfcol调用,并为所有图创建一个全局调用,例如:

---
title: "My presentation"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## PLOTS

```{r}
list_genes <- c("GEN1", "GEN2", "GEN3")

myfunction <- function(x,y){
  for(gene in list_genes){
    stripchart(x, method = "jitter", vertical = FALSE, 
               main = paste0("Plot of ", gene))
    hist(x, main = paste0("Plot of ", gene))
  }
}

par(mfcol = c(2, 3))
myfunction(x = c(1, 5, 6, 2, 4, 30, 23, 12, 45))
```

带有 6 个图的幻灯片

An additional note: It is better to use FALSE and not F in R code.附加说明:R代码中最好使用FALSE而不是F FALSE is a reserved word while F is "fragile" as it can be re-defined. FALSE是保留字,而F是“脆弱的”,因为它可以重新定义。

Thanks to @tpetzoldt and this post , I found exactly what I needed it!感谢@tpetzoldt 和这篇文章,我找到了我需要的东西!

I had to change a bit the function and create the headers of the presentation inside the loop.我不得不稍微改变一下函数并在循环内创建演示文稿的标题。

Here is the solution:这是解决方案:

---
title: "Create a presentation with several plots"
output:
    ioslides_presentation
---

```{r, echo=FALSE}
myfunction <- function(x, gene){
    # This to draw both plots
      par(mfrow=c(2,1))  
      
      stripchart(x, method="jitter", vertical=F, main=paste0("Plot of ", gene))
      
      hist(x, main=paste0("Plot of ", gene))
}
```

```{r, echo=FALSE}
list_genes <- c("GEN1", "GEN2", "GEN3")
```


```{r, echo = FALSE, results = "asis"}
for(gene in list_genes){
  cat("\n\n## Plot of ", gene, "\n\n")
  myfunction(x=c(1,5,6,2,4,30,23,12,45), gene)
}
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM