Combine kableExtra tables with ggplot2 elements
Asked Answered
M

1

6

I have a ggplot2 element and a KableExtra table. Is it possible to combine them into one object with patchwork, cowplot or any other package and print it in an rmarkdown document? If there's not a solution, is there any other table package, with html or latex design, which I can use to solve this issue?

Is it possible to use custom blocks (https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html) to join these elements together? I tried without success.

Adding the table to ggplot as a custom_annotation won't work for me as I want to add many tables and plots to the same element.

> dt_example <- data.table(index = c('type', 'model', 'manufacturer', 'time', 'revolutions', 'element', 'fluid'), object = c('B12', 'Schullo', 'GM', '5 h', '54 k', 'XX', 'water'))
> dt_example
          index  object
1:         type     B12
2:        model Schullo
3: manufacturer      GM
4:         time     5 h
5:  revolutions    54 k
6:      element      XX
7:        fluid   water

kable_table <- dt_example %>% 
    kable(col.names = c('',''), format = 'html', table.attr = "style='width:30%;'") %>%
    kable_styling() %>%
    pack_rows('model', 1, 5, label_row_css = "background-color: #FBFBFB;") %>%
    pack_rows('column', 6, 6, label_row_css = "background-color: #FBFBFB;") %>%
    pack_rows('fluid', 7, 7, label_row_css = "background-color: #FBFBFB;")

kable_table

which gives me the table below:

kable_table_output

The ggplot element:

ggplot_element <- ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5)

ggplot_element

And its output:

ggplot2_output

The desired output of the combination should be:

enter image description here

Meredi answered 20/7, 2020 at 6:19 Comment(0)
C
0

The answer is yes and no. Maybe.

I'm assuming that when you say "combine" you mean "display side-by-side" rather than "unite into a single R object".

Using pure Markdown, the answer is no, as explained here: "Markdown’s formatting syntax only addresses issues that can be conveyed in plain text". However, as you hint, there are other solutions. raw CSS and HTML being two. But CSS (and, to an extent, HTML) solutions depend both on the browser displaying your content and the server providing it. Some browsers don't support some CSS elements, some servers restrict what you're able to do. From GitHub, for example: "The HTML is sanitized, aggressively removing things that could harm you and your kin—such as script tags, inline-styles, and class or id attributes".

That being said,

---
title: "Untitled"
output: html_document
---

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

```{css, echo=FALSE}
.rightColumn {display: inline-block; color: green;}
.leftColumn {display: inline-block; color: red;}
.columnContainer {
   -webkit-column-count: 2; -moz-column-count: 2; column-count: 2; 
   -webkit-column-rule: 1px dotted #e0e0e0; -moz-column-rule: 1px dotted #e0e0e0; 
   column-rule: 1px dotted #e0e0e0;
}
```
```{css, echo=FALSE}
<div class="columnContainer">
```
```{r, echo=FALSE, class.output="leftColumn"}
print("Left column")
```
```{r, echo=FALSE, class.output="rightColumn"}
print("Right column")
```
```{css, echo=FALSE}
</div>
```

produces the following output for me:

enter image description here

Which demonstrates that what you want is possible in principle. Unfortunately, the CSS hierarchy of a markdown document is complex (for me - I'm not an expert) and I've not been able to replicate the effect with kable output and an image in the two columns.

Replacing print("Right column") with mtcars %>% head() produces the desired effect but mtcars %>% kable() does not. I've not been able to make any progress with an image.

I hope that helps. Perhaps someone else can provide the final details.

Cordula answered 20/7, 2020 at 7:32 Comment(1)
Any solutions with another type of table that can reproduce the same style as the table in the question?Meredi

© 2022 - 2024 — McMap. All rights reserved.