Skip to content

Instantly share code, notes, and snippets.

@jennybc
Last active June 30, 2022 19:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennybc/e9e9aba6ba18c72cec26 to your computer and use it in GitHub Desktop.
Save jennybc/e9e9aba6ba18c72cec26 to your computer and use it in GitHub Desktop.
Quicky and dirty: side-by-side plot and table
---
title: "Quicky and dirty: side-by-side plot and table"
author: "Omar AlOmeir"
output:
html_document:
css: two_columns.css
---
Code from Omar AlOmeir, posted by Jenny Bryan
See <a href="https://gist.github.com/jennybc/e9e9aba6ba18c72cec26#file-2015-03-02_plot-next-to-table-rmd" target='_blank'>this Gist</a> for the code.
See [this RPub](http://rpubs.com/jennybc/plot-next-to-table) for the rendered HTML.
```{r}
library(ggplot2)
suppressMessages(library(dplyr))
#devtools::install_github("jennybc/gapminder")
library("gapminder")
gtbl <- tbl_df(gapminder)
```
<div class="twoC">
```{r results = 'asis', echo=FALSE}
tbl1 <- gtbl %>%
group_by(continent) %>%
summarise_each(funs(mean, median), gdpPercap)
knitr::kable(tbl1)
```
```{r echo=FALSE}
ggplot(gtbl, aes(x = continent, y = gdpPercap, color = continent)) +
geom_point() + ggtitle("GDP per cap by continent")
```
</div>
<div class="clearer"></div>
.twoC {width: 100%}
.clearer {clear: both}
.twoC .table {max-width: 50%; float: right}
.twoC img {max-width: 50%; float: left}
@min110
Copy link

min110 commented Oct 9, 2015

my table is messy then , bad luck?

@jennybc
Copy link
Author

jennybc commented Sep 26, 2016

I gather this came from stack overflow? Not sure? But I'd be happy to link to the original source if someone knows.

@jennybc
Copy link
Author

jennybc commented Apr 14, 2017

More hackage: put this directly in the Rmd:

<style type="text/css">
.twoC {width: 100%}
.clearer {clear: both}
.twoC .table {max-width: 50%; float: right}
.twoC img {max-width: 50%; float: left}
</style>

@phillip-toone
Copy link

It looks like this only works for html output? I would like to place a table and a plot side by side in a pdf document. Any suggestions? I can accomplish this using tableGrob but would prefer to use kable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment