0

There are already a few questions considering ggplots in RMarkdown but none has answered my question as how to put a ggplot into a table with kable() by knitr. I ve tried this link:

How can I embed a plot within a RMarkdown table?

But have not had any luck so far. Any ideas?

The idea was to put all plots into a list with

a<-list(p1,p2,p3...)

and then having the table with

{r}kable(a)

Additional text should also be able to be included

b<-("x","y","z",...)
kable (c(a,b),col.names=c())

Thanks for your help

Frieder

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Can you maybe explain why you want the plots in a table maybe it is sufficiënt to use sub plots and sub captions like explained [here](https://stackoverflow.com/questions/12546365/subfigures-or-subcaptions-with-knitr?noredirect=1&lq=1)? – Kevin Jun 02 '20 at 13:49
  • That is unfortunatly the requirement. It needs to be in a format of a table. I want to replace manual creation of Word Docs to save time. – Frieder Jun 03 '20 at 07:25

2 Answers2

0

I experimented some with this and the following is the best I could come up with. This is a complete markdown document you should be able to paste into RStudio and hit the Knit button.

Two relevant notes here.

  • Setting the file links directly into kable doesn't work as it is wrapped in html such that it is interpreted as text, so we need to gsub() it in. An alternative is to set kable(..., escape = FALSE), but it is a risk that other text might cause problems.
  • Also, the chunk option results = 'asis' is necessary to have the print(kab) return raw html.

I don't know if these are problems for the real application.

---
title: "Untitled"
author: "me"
date: "02/06/2020"
output: html_document
---

```{r, results = 'asis'}
library(ggplot2)
library(svglite)

n <- length(unique(iris$Species))
data <- split(iris, iris$Species)

# Create list of plots
plots <- lapply(data, function(df) {
  ggplot(df, aes(Sepal.Width, Sepal.Length)) +
    geom_point()
})

# Create temporary files
tmpfiles <- replicate(n, tempfile(fileext = ".svg"))

# Save plots as files, get HTML links
links <- mapply(function(plot, file) {
  # Suit exact dimensions to your needs
  ggsave(file, plot, device = "svg", width = 4, height = 3)
  paste0('<figure><img src="', file, '" style = "width:100%"></figure>')
}, plot = plots, file = tmpfiles)

# Table formatting
tab <- data.frame(name = names(plots), fig = paste0("dummy", LETTERS[seq_len(n)]))
kab <- knitr::kable(tab, "html")

# Substitute dummy column for figure links
for (i in seq_len(n)) {
  kab <- gsub(paste0("dummy", LETTERS[i]), links[i], kab, fixed = TRUE)
}
print(kab)
```
teunbrand
  • 33,645
  • 4
  • 37
  • 63
0

I have found my way around it as described in the link I posted.

I. Saved my plot as a picture II. Used sprintf() to insert picture into table with this command from Rmarkdown: ![](path/to/file)

Poor, but it works. If anybody finds a solution, I will always be interested in smart coding.