0

I was wondering if anyone knows how to add a footnote beneath an Xtable, as well as seperating each column with a vertical line to make this table look a bit neater.

I would like my footnote to say "these are ADF test results"

I've reproduced my code and dataframe below

{r table_3, echo = FALSE, warning = FALSE, message = FALSE, results = 'asis'}
library(tidyverse)
library(tidyselect)
library(xtable)
library(readxl)
library(knitr)

# Create Dataframe


variables <- c("Argentina Bond Flows", "Argentina Equity Flows", "Chile Bond Flows", "Chile Equity Flows", "Mexico Bond Flows",
               "Mexico Equity Flows", "Indonesia Bond Flows", "Indonesia Equity Flows", "Korea Bond Flows", "Korea Equity Flows",
               "Philippines Bond Flows", "Philippines Equity Flows", "Thailand Bond Flows", "Thailand Equity Flows", 
               "South Africa Bond Flows", "South Africa Equity Flows","United States S&P 500", "United States M1")


adf <- c("-4.3557","-6.4865","-3.4893","-3.3485","-2.6294","-5.511","-2.9238","-6.0305","-9.7081","-2.0444","-4.7619","-5.6108","-4.6314","-4.6218","-4.3337","-3.7213","2.6471"," 4.0174")
pp <- c("-6.9685","-9.3864","-6.7449","-5.6533","-6.0265","-9.4065","-6.531","-7.5043","-21.0764","-3.5716","-8.3505","-6.5574 ","-10.3266","-7.445","-8.9639","-6.3464","-11.1298","4.0512")

variables2 <- c("Argentina M1", "Argentina Equity Index", "Chile M1", "Chile Equity Index", "Mexico M1",
                "Mexico Equity Index", "Indonesia M1", "Indonesia Equity Index", "Korea M1", "Korea Equity Index",
                "Philippines M1", "Philippines Equity Index", "Thailand M1", "Thailand Equity Index", 
                "South Africa M1", "South Africa Equity Index","", "")


adf2 <- c("-6.9593","6.1785","3.6245","-2.2697","3.2417","2.4172","2.4237","2.0484","3.8211","-6.938","-6.9593","-7.7315","-6.9593","6.1785","-8.0415 ","2.5385","","")
pp2 <- c("-13.7725","4.8728","-11.0177","-9.3776","-14.3688 ","-2.2334","-13.1095","-8.8171","-11.1806","-9.8397","-13.7725","-9.4015","-2.6801","-11.8756","-10.681"," -9.6241","","")


unit_table <- data.frame(variables, adf, pp,variables2, adf2,pp2)
colnames(unit_table) <- c("Country Variable", "ADF", "PP","Country Variable", "ADF", "PP")

# Create a table using XTable

table <- xtable(unit_table, caption = "Unit Root Test Results \\label{Table3}",
                # tabular.environment = "longtable",
                floating = TRUE,
                table.placement = 'H',
                include.rownames = FALSE,
                # scalebox = 0.3,
                comment = FALSE,
                caption.placement = 'top'
                 )
bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')}


print(table, include.rownames = FALSE, sanitize.colnames.function=bold, comment = FALSE)

Also, how do I go about boldifying certain words in the table. For example if I wanted to boldify "Korea Bond Flows", how would I go about doing that?

TIA!

Tanga94
  • 695
  • 6
  • 27

1 Answers1

0

I don't use xtable much but have a few suggestions that might be helpful.

First, to add a footnote, you can include the following:

comment <- list(pos = list(0))
comment$pos[[1]] <- c(nrow(unit_table))
comment$command <- c(paste("\\hline\n", 
                           "These are ADF test results.\n", 
                           sep = ""))

And then when you print the table, add add.to.row = comment, hline.after = c(-1, 0). For more details on this, see similar question.

To separate columns with vertical lines, use align and provide pattern with alignment (left or right, for example) and where you would like divisions. When you call the xtable function add option align = "ll|l|l|l|l|l" for example.

To bold specific cells in the table, add a code for those table elements, such as BOLD for Korea Bond Flows:

unit_table[9, "Country Variable"] <- paste0("BOLD", unit_table[9, "Country Variable"])

And use a function to detect this BOLD code:

bold.function <- function(x) gsub('BOLD(.*)', paste('\\\\textbf{\\1','}'),x)

And add sanitize.text.function = bold.function to your print of the xtable.

Here is the entire RMarkdown that seems to work. Of note, I escaped your & in United States S&P 500 and added stringsAsFactors = FALSE when creating the data frame.

---
title: "Test"
output: pdf_document
classoption: landscape
---

```{r, results="asis", echo = FALSE}

library(xtable)

variables <- c("Argentina Bond Flows", "Argentina Equity Flows", "Chile Bond Flows", "Chile Equity Flows", "Mexico Bond Flows",
               "Mexico Equity Flows", "Indonesia Bond Flows", "Indonesia Equity Flows", "Korea Bond Flows", "Korea Equity Flows",
               "Philippines Bond Flows", "Philippines Equity Flows", "Thailand Bond Flows", "Thailand Equity Flows", 
               "South Africa Bond Flows", "South Africa Equity Flows","United States S\\&P 500", "United States M1")

adf <- c("-4.3557","-6.4865","-3.4893","-3.3485","-2.6294","-5.511","-2.9238","-6.0305","-9.7081","-2.0444","-4.7619","-5.6108","-4.6314","-4.6218","-4.3337","-3.7213","2.6471"," 4.0174")
pp <- c("-6.9685","-9.3864","-6.7449","-5.6533","-6.0265","-9.4065","-6.531","-7.5043","-21.0764","-3.5716","-8.3505","-6.5574 ","-10.3266","-7.445","-8.9639","-6.3464","-11.1298","4.0512")

variables2 <- c("Argentina M1", "Argentina Equity Index", "Chile M1", "Chile Equity Index", "Mexico M1",
                "Mexico Equity Index", "Indonesia M1", "Indonesia Equity Index", "Korea M1", "Korea Equity Index",
                "Philippines M1", "Philippines Equity Index", "Thailand M1", "Thailand Equity Index", 
                "South Africa M1", "South Africa Equity Index","", "")

adf2 <- c("-6.9593","6.1785","3.6245","-2.2697","3.2417","2.4172","2.4237","2.0484","3.8211","-6.938","-6.9593","-7.7315","-6.9593","6.1785","-8.0415 ","2.5385","","")
pp2 <- c("-13.7725","4.8728","-11.0177","-9.3776","-14.3688 ","-2.2334","-13.1095","-8.8171","-11.1806","-9.8397","-13.7725","-9.4015","-2.6801","-11.8756","-10.681"," -9.6241","","")

unit_table <- data.frame(variables, adf, pp,variables2, adf2,pp2, stringsAsFactors = FALSE)
colnames(unit_table) <- c("Country Variable", "ADF", "PP","Country Variable", "ADF", "PP")

# Create a table using XTable

unit_table[9, "Country Variable"] <- paste0("BOLD", unit_table[9, "Country Variable"])

table <- xtable(unit_table, 
                caption = "Unit Root Test Results \\label{Table3}",
                # tabular.environment = "longtable",
                floating = TRUE,
                table.placement = 'H',
                include.rownames = FALSE,
                # scalebox = 0.3,
                comment = FALSE,
                caption.placement = 'top',
                align = "ll|l|l|l|l|l"
)

# Additional code added

bold.function <- function(x) gsub('BOLD(.*)', paste('\\\\textbf{\\1','}'),x)

comment <- list(pos = list(0))
comment$pos[[1]] <- c(nrow(unit_table))
comment$command <- c(paste("\\hline\n", 
                           "These are ADF test results.\n", 
                           sep = ""))

print(table, add.to.row = comment, hline.after = c(-1, 0), sanitize.text.function = bold.function)
```
Ben
  • 28,684
  • 5
  • 23
  • 45
  • Thank you so much @Ben, this is incredibly helpful! One more thing, any advice on how to rotate the table to be able to fit on a page? – Tanga94 Dec 16 '19 at 09:25
  • I did add `classoption: landscape` above to show .pdf in landscape format so table would fit on page. If your .pdf is a long report and want to include both portrait and landscape orientations, see this [link](https://stackoverflow.com/questions/25849814/rstudio-rmarkdown-both-portrait-and-landscape-layout-in-a-single-pdf). For alternatives with `kable` or `stargazer` take a look at [this](https://stackoverflow.com/questions/21840878/rotate-a-table-from-r-markdown-in-pdf) or [this](https://stackoverflow.com/questions/11777586/customize-xtable). Hope this is helpful. – Ben Dec 16 '19 at 14:41
  • Thank you @Ben, I managed to get it to be landscape using a package called "rotating" in the header of my document. Any way around the fact that the footer in your solution is also seperated by the the vertical lines from the align command? I've noticed that I can't insert a long footnote because it assumes it is part of my first column, so the longer my footnote is, the more my first column expands (and I would like to avoid this). – Tanga94 Dec 18 '19 at 16:03
  • I'm sorry, I don't know. I understand the footnote makes use of an additional row, and that could conflict with vertical lines in the table. I checked your other post and haven't seen any answers (yet). Have you looked at other packages besides `xtable` for table development, that might offer a better fit for your customization needs? – Ben Dec 19 '19 at 14:42