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
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>