[Tex/LaTex] Tables with multiple panels in LaTeX, R and Sweave

horizontal alignmentrsweavetables

I would like to know what the accepted Sweave/R/LaTeX solution is for producing tables with "panels": multiple parts, which may or may not contain the same columns, combined into one table. The panels should be of equal width, even if they have different columns, without having to resize so that the font looks different). I would like this to work for any combination of data frames, not just model results (as discussed here). Right now, for instance, I am working on combining tables of customized descriptive statistics across multiple data frames.

Multiple tables within one table environment is possible using subfig (as explained here). This solution works fine if you do not need the tables to line up with one another or be enclosed in one large border (as far as I can tell). It also does not allow the use of threeparttable, which makes it possible to modify the caption and add footnotes (or, at least, threeparttable seems unwilling to accept multiple tabular environments like those produced by xtable).

I have checked out the Hmisc package, particularly the latex commands, but they mostly seem to accept one object. Besides, this seems like a problem that should be solved primarily in LaTeX, since it involves table formatting more than it involves data manipulation.

This seems like a straightforward and common problem, but I cannot seem to find the answer anywhere online. Everyone discusses using threeparttable and subfig to solve separate problems of customizing table appearance and including multiple tables, but not in combination. Searching Google for "panels" in LaTeX or R does not produce the information I am looking for: most of the hits discuss panel data or charts with multiple panels. Perhaps there is another term for these that is being used?

Anyway, I have been banging away at this for a little while, and I would appreciate any suggestions.

Edit: Alan asked for an example of what I am looking for. One of the tables that I am looking for would be the following: a combined table showing Descriptive Statistics and Correlations.

              Table X: Descriptive Statistics and Correlations for Variables        
---------------------------------------------------------------------------------------
Panel A: Descriptive Statistics
                Mean    Median    St. Dev.    IQR    25th Percentile    75th Percentile
Variable 1
Variable 2
...
----------------------------------------------------------------------------------------
Panel B: Correlations
              Var. 1     Var. 2     Var. 3     Var. 4     Var. 5      Var. 6     Var. 7
Variable 1
Variable 2
Variable 3
Variable 4
Variable 5
Variable 6
Variable 7
-----------------------------------------------------------------------------------------
  [1] Footnote 1: Variable 1 is calculated as...
  [2] Correlations are Pearson.

My R code would just produce these two data frames: currently, I am literally having Sweave xtable these pre-made frames to external .tex files and then pulling them in using \subfloat.

Note that the two tables have different sizes and numbers of columns, but come to the same width. I see these types of tables all the time, with many different combinations of panels, so I know that there must be an easy way to do this in LaTeX.

Hope this helps. Please let me know if I can provide additional information. I can provide R and Sweave code if necessary, but that seems like it could get more confusing than helpful.

Best Answer

Here is a possible solution to your similar-width panel in tables by means of the tabularx package. It provides the environment tabularx that takes an argument specifying the width of the tabularx. That allows one to typeset the two panels as two separate tables but still maintain the same width across both tables. To stretch it across the entire width of the text, I used \linewidth. A new column type Y was defined as a right-aligned (\raggedleft) version of the stretchable X column type provided by tabularx. Also, the booktabs package was loaded in order to improve the table layout.

\documentclass{article}
\usepackage[margin=2cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}% raggedleft column X
\begin{document}

\begin{table}[htb]
  \caption{Descriptive Statistics and Correlations for Variables}
  \label{tbl:stats-and-correlations}
  \begin{tabularx}{\linewidth}{l*{6}{Y}}
    \toprule
    \multicolumn{7}{l}{\textbf{Panel A: Descriptive Statistics}} \\
    \midrule
               & Mean & Median & St.\ Dev. & IQR & 25$^{\textrm{th}}$ & 75$^{\textrm{th}}$ \\[0pt]
               &      &        &           &     & Percentile         & Percentile \\
    Variable 1 & 12.3 & 45.6   & 7.89      &  5  & 12.0                          & 0.22 \\
    Variable 2 &  8.3 &  1.0   & 0.01      & 12  & 99.9                          & 10.0 \\
    \ldots     &      &        &           &     &                               &      
  \end{tabularx}
  \begin{tabularx}{\linewidth}{l*{7}{Y}}
    \toprule
    \multicolumn{7}{l}{\textbf{Panel B: Correlations}} \\
    \midrule
               & Var.\ 1 & Var.\ 2 & Var.\ 3 & Var.\ 4 & Var.\ 5 & Var.\ 6 & Var.\ 7 \\
    Variable~1 &    0.78 &    0.37 &    0.48 &    0.10 &    0.13 &    0.58 &    0.41 \\
    Variable~2 &    0.46 &    0.86 &    0.96 &    0.44 &    0.15 &    0.56 &    0.31 \\
    Variable~3 &    0.03 &    0.75 &    0.11 &    0.44 &    0.71 &    0.06 &    0.26 \\
    Variable~4 &    0.21 &    0.25 &    0.38 &    0.88 &    0.24 &    0.52 &    0.46 \\
    Variable~5 &    0.20 &    0.93 &    0.54 &    0.96 &    0.55 &    0.82 &    0.62 \\
    Variable~6 &    0.67 &    0.85 &    0.74 &    0.99 &    0.27 &    0.48 &    0.85 \\
    Variable~7 &    0.82 &    0.89 &    0.68 &    0.06 &    0.02 &    0.30 &    0.10 \\
    \bottomrule
  \end{tabularx}

  [1]\ Footnote 1: Variable~1 is calculated as\ldots \endgraf
  [2]\ Correlations are Pearson.
\end{table}

\end{document}

Table with "panels"

Minor adjustments to the column alignment, spacing and typesetting is possible.