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))
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))
```
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
CodePudding user response:
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))
```
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.
CodePudding user response:
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)
}
```