19

I'm writing in R Markdown and have a contingency table that is quite wide. I am converting the R markdown document to a PDF using pandoc.

Is it possible to rotate or shrink the table? Ideally this would be done without having to switch to LaTeX formatting.

My Attempts:

I've been abusing the figure options in knitr to attempt this, but whether I use kable or xtable, I haven't had any luck. Some permutations I have tried include:

```{r out.extra='angle=90', results='asis'}
library(knitr)
kable(iris[1:5,])
``` 

``{r size='footnotesize', results='asis'}
library(knitr)
kable(iris[1:5,])
```

```{r out.extra='angle=90', results='asis'}
library(xtable)
xtable(iris[1:5,])
```

```{r size='footnotesize', results='asis'}
library(xtable)
xtable(iris[1:5,])
```  

All of these show the table nicely, but do not rotate it.

The code I'm using to knit is:

Rscript -e "library(knitr); knit('table.Rmd', 'table.md')"

And to convert to pdf:

pandoc table.md -o table.pdf
5
  • An alternative "solution" would be using pander that can split "too wide" tables into multiple pieces.
    – daroczig
    Feb 17, 2014 at 23:08
  • My actual table is a contingency table, so it's important to be able to read all the way across it for comparison.
    – sus
    Feb 18, 2014 at 0:05
  • 1
    You could try plotting the table as an image something like this, which could presumably be more easily manipulated. Sep 15, 2014 at 19:30
  • Thanks Gregor! This was the approach that worked for me!
    – sus
    Nov 6, 2014 at 0:40
  • 1
    This works: stackoverflow.com/questions/25849814/…
    – Jason
    Feb 1, 2016 at 16:52

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

15

The out.extra='angle=90' only works on Figures, and unfortunately not tables. Here are several potential approaches:

KableExtra (Rotate Page)

You can easily rotate tables using the useful addon package kableExtra. Specifically, the landscape() function will put the table on an single landscape page. It’s useful for wide tables that can’t be printed on a portrait page.

library(kableExtra)

kable(iris[1:5,],
      format = "latex", booktabs = TRUE) %>%
  kableExtra::landscape()

The limitation of these function is that it does force a new page, so depending on the size of your table it could leave a bit of blank space.

KableExtra (Scale Width)

You can scale the width of the table using the function kable_styling(latex_options = "scale_down"). This will force the table to the width of the page.

   kable(iris[1:5,],
          format = "latex", booktabs = TRUE) %>%
          kable_styling(latex_options = "scale_down")

For more examples of the kableExtra package, check out the package here: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

Stargazer (Rotate Table)

Other options are available, but these largely require the installation of additional LaTeX packages. For example, the stargazer package can print tables in landscape using the float.env argument:

```{r, results="asis"}
stargazer(iris[1:5,], 
          float.env = "sidewaystable")
```

This requires \usepackage{dcolumn} in LaTeX preamble

Read more about customising your LaTex preamble here: https://tex.stackexchange.com/questions/171711/how-to-include-latex-package-in-r-markdown

4
  • kableExtra just gives me ## Currently generic markdown table using pandoc is not supported. Dec 18, 2017 at 20:03
  • 1
    You probably haven't said format = "latex" within the kable function. Dec 18, 2017 at 21:26
  • 1
    Yes, that was it. Dec 19, 2017 at 15:12
  • 2
    I came across this solution first when looking for how to implement sidewaystables with knitr and stargazer but couldn't get it to work using \usepackage{dcolumn}. Instead, I found in this response that I had to use \usepackage{rotating}: stackoverflow.com/a/34292292/5577741
    – fmerhout
    Oct 12, 2018 at 13:47
2

You can add some LATEX code in your Rmd file :

\usepackage{lscape}
\usepackage{pdfpages}

Some text on a portrait page.

\newpage
\blandscape

## Title, lorem ipsum

```{r, results = "asis"}
kable(iris[1:5,], caption = "Lorem again")
```
Lorem ipsum...

\elandscape

Some other text on a portrait page.
0

Can you just use t()?

library(xtable)
xtable(t(iris[1:5,]))

If your table is still to long, split it up into multiple tables. e.g.:

splits = floor(seq(1, ncol(iris), length=5))
for(i in 2:length(splits)){
 mini.tab = iris[ , splits[i-1]:splits[i]]
 xtable(mini.tab)
}
2
  • While that works fine for this example, my actual table is still too wide transposed!
    – sus
    Feb 18, 2014 at 0:03
  • Yes, splitting the table would normally be a good option, but my table is a contingency table, so splitting it makes it much less comprehensible.
    – sus
    Feb 19, 2014 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.