2

I want to place a table next to a plot (plot is based on the very table) and ideally visually align the table rows with the ticks of the plot. Currently I am playing with theme(plot.margin) and fig.height to try to do that by hand and results are OKish, but as soon as something changes (adding legends to the plot, or rows to the table) I have to re-iterate.

Are there any better approaches to achieve the same results in a more automated way? Maybe adding the table to the plot?

Screenshots

With manual alignment Aligned

Without manual alignment Unaligned

Code

---
output: html_document
---

```{r setup, include = FALSE}
library(kableExtra)
library(tibble)
library(dplyr)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE,
                      message = FALSE,
                      warning = FALSE,
                      fig.align = "center")
```

<div class = "row">
<div class = "col-md-3">
```{r}
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg)
kable(raw_dat) %>%
   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                 full_width = FALSE)
```
</div>
<div class = "col-md-9">
```{r, fig.height=5.5}
   ggplot(raw_dat, aes(factor(id, rev(id)), mpg)) +
   geom_point() +
   coord_flip() +
   theme(plot.margin = margin(0.6, unit = "cm"))
```
</div>
</div>
thothal
  • 16,690
  • 3
  • 36
  • 71
  • 1
    Does anything from [this post](https://stackoverflow.com/questions/53659160/r-markdown-positioning-table-and-plot-side-by-side) help? – chemdork123 Mar 26 '20 at 13:51
  • 1
    Thanks for the pointer. But this post describe only how to put the table and the plot side by side (which I know how to do at least for the HTML output). However, I was wondering if there is a way how to align them row by tick, so to speak. – thothal Mar 26 '20 at 13:55
  • I see - you want to make the height the same for table and plot? – chemdork123 Mar 26 '20 at 15:03
  • 1
    Not necessarily the height, but ideally the tick mark for `Mazda` for instance is exactly in the middle of the row of `Mazda`. – thothal Mar 27 '20 at 23:43

0 Answers0