1

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))

image1

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).

---
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))
```

image2

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). 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.

Does anyone know how to solve it?

Thanks very much in advance

Regards

emr2
  • 1,436
  • 7
  • 23

2 Answers2

2

Thanks to @tpetzoldt and this post, I found exactly what I needed it!

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)
}
```
emr2
  • 1,436
  • 7
  • 23
1

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, e.g.:

---
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))
```

slide with 6 plots

An additional note: It is better to use FALSE and not F in R code. FALSE is a reserved word while F is "fragile" as it can be re-defined.

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
  • Thanks very much for your help. It looks very nice, but... I need to have the plots individually, I don't want to collate them. With my original function I generate more than 30 plots, I cannot create a global one with all of them since I won´t be able to see anything... Do you know how to do it in another way, not pasting all the plots in one global one? – emr2 Nov 16 '21 at 17:51
  • There are several methods: (a) define the plot function in one code chunk and then call it in several code chunks, each on a single slide; (b) save the plots in the code chunks to files and then import it later with standard markdown syntax; (c) use scrollable "long slides"; (d) create a whole markdown file or parts of it programmatically. Which of the options do you prefer? – tpetzoldt Nov 16 '21 at 18:34
  • mmm option c) would be interesting, although I have never used css, so I don't know how to do it. And.. what did you mean with the option d) ? I would like to automatize everything if it is possible. Could you help me, please? I have been with this a long time and I am bit desperate.... (Thanks very much!) – emr2 Nov 16 '21 at 21:59
  • I have checked the option c) and I found this post https://stackoverflow.com/questions/53064555/how-to-make-scrollable-slides-in-an-ioslides-presentation-with-rmarkdown I was expecting another thing, but it is nice. Thanks! Btw, with the option b) each plot has to be saved in several chunks of code, right? they cannot be saved in a list and then do a for loop and generate the slides... – emr2 Nov 17 '21 at 09:02
  • I found the solution that I needed it checking a bit more of your option d). Thanks very much!!!! – emr2 Nov 17 '21 at 10:35