I have a few ggplot2 plots stored in a named list, plt_list, and I would like to display the plots in R or Rmarkdown, without the names or list indices (e.g. [[1]]) to be displayed; just the plots. I have tried unname(plt_list), but the indices are printed into the console (or in Rmarkdown document, before each plot). With invisible nothing is displayed. Is there any way to do this in R?
CodePudding user response:
We can use walk from purrr to display in Rmarkdown
---
title: "Title"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r plot_create, echo = FALSE}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(purrr))
p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) geom_col()
plt_lst <- list(p1 = p1, p2 = p1, p3 = p1)
```
```{r plots, echo = FALSE, results = 'asis'}
walk(plt_lst, print)
```
-output
If we are trying this in R console, a for loop should also work
for(i in seq_along(plt_lst)) plt_lst[[i]]

