【问题标题】:R: Print two tables with xtable ()R:用 xtable() 打印两张表
【发布时间】:2011-06-07 09:42:31
【问题描述】:

我有数据表(d1 和 d2),我想在乳胶中并排或叠放打印,并带有各自的标题。可以直接用xtable() 做到这一点吗?这两个表应该是不同的,即我们可以称它们为 Table x(a)Table x(b),但它们应该是相邻的或堆叠的。 p>

【问题讨论】:

  • 一张桌子?那么xtable(rbind(d1,d2))xtable(cbind(d1,d2)) 应该没问题吧?
  • 如果您想将表分开为 Xa 和 Xb,那么这更像是一个乳胶标签问题。这里的一位版主应该能够迁移您的问题。
  • @SachaEpskamp rbind 仅在两个表具有相同的ncol(或cbind / nrow)时才有效。

标签: r sweave


【解决方案1】:

我建议将结果保存为不同文件中的两个单独的表(请参阅print.xtable()file= 选项),然后使用您认为适合您的布局的任何命令将它们input 保存到您的 LaTeX 文档中(@ 987654327@、subfloatminipage 等)。这是我通常所做的,尽管我通常依赖 Hmisc 包中的 LaTeX 工具。如果您只想将它​​们打印为独立的 PDF,请为您的文档使用 standalone 类。

所以,这里有一个例子:

data(tli)
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
print(xtable(fm1), file="ta.tex", floating=FALSE)
print(xtable(head(tli, n=5)), file="tb.tex", floating=FALSE)

然后,一个快速的 tex 包装器(使用 pdflatex 编译):

\documentclass{article}
\usepackage{subfig}
\usepackage{graphicx}

\begin{document}

\begin{table}[ht]
\centering
\subfloat[Table x(a)]{\label{tab:tab1a}\scalebox{.5}{\input{./ta}}}\quad
\subfloat[Table x(b)]{\label{tab:tab1b}\scalebox{.5}{\input{./tb}}}
\caption{Caption about here}
\label{tab:tab1}
\end{table}

\end{document}

结果如下:

删除默认(堆叠)布局的\scalebox 命令,除非它们足够窄以适应默认大小,如@David 所述。

【讨论】:

  • @chi 删除 \scalebox 如果表格足够窄以适合其默认大小,则不会堆叠表格。
  • @David 啊,我认为你是对的。相应地更新了我的回复!
【解决方案2】:

Alan Munn's answer to a similar question on tex.stackexchange.com

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}
\title{Side-by-side xtables}
\author{}
\date{}
\begin{document}
\maketitle
First some R code to create some data.
<<>>=
myData <- matrix(c(19,89,23,23,74,44,16,39,67),ncol=3,byrow=TRUE)
colnames(myData) <- c("A","B","C")
rownames(myData) <- c("1","2","3")
myData2 <- myData * 2
@

Now we place the data in two side-by-side tables:

\begin{table}[htb]
\begin{minipage}{.45\textwidth}
\centering
<<echo=FALSE,results=tex>>=
library("xtable")
print(xtable(myData),
  floating=FALSE,
  hline.after=NULL,
  add.to.row=list(pos=list(-1,0, nrow(myData)),
  command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\captionof{table}{The first table}
\end{minipage}
\begin{minipage}{.45\textwidth}
\centering
<<echo=FALSE,results=tex>>=
print(xtable(myData2),
  floating=FALSE,
  hline.after=NULL,
  add.to.row=list(pos=list(-1,0, nrow(myData2)),
  command=c('\\toprule\n','\\midrule\n','\\bottomrule\n')))
@
\captionof{table}{The second table}
\end{minipage}
\end{table}
\end{document}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多