R:使用xtable()打印两个表
我有数据表(d1和d2),我想用乳胶将它们并排打印,或在彼此的顶部打印,并带有各自的标题。是否可以直接使用R:使用xtable()打印两个表,r,sweave,R,Sweave,我有数据表(d1和d2),我想用乳胶将它们并排打印,或在彼此的顶部打印,并带有各自的标题。是否可以直接使用xtable()?这两个表应该是不同的,即我们可以将它们称为表x(a)和表x(b),但它们应该是相邻的或堆叠的。我建议将结果保存为不同文件中的两个单独的表(请参见文件=选项以打印.xtable()),然后使用您认为适合您的布局的任何命令(表格,子文件,迷你页,等等)将它们输入到LaTeX文档中。这是我通常做的,尽管我通常依赖于包中的乳胶设施。如果您只想将其作为独立PDF打印,请为您的文档使用
xtable()
?这两个表应该是不同的,即我们可以将它们称为表x(a)和表x(b),但它们应该是相邻的或堆叠的。我建议将结果保存为不同文件中的两个单独的表(请参见文件=
选项以打印.xtable()
),然后使用您认为适合您的布局的任何命令(表格,子文件,迷你页,等等)将它们输入到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)
结果如下:
删除默认(堆叠)布局的\scalebox
命令,除非它们足够窄,适合其默认大小,如@David所述
请参见
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{caption}
\标题{并排xtables}
\作者{}
\日期{}
\开始{document}
\maketitle
首先使用一些R代码来创建一些数据。
=
myData作为一个表?然后xtable(rbind(d1,d2))
或xtable(cbind(d1,d2))
应该可以,对吧?如果您想将表作为Xa和Xb分开,那么这就成了一个更大的问题。这里的一位主持人应该能够迁移您的问题。@SachaEpskamprbind
只有在两个表都有相同的ncol
(或者对于cbind
/nrow
)时才起作用。@chi删除\scalebox
,如果表窄到适合其默认大小,则不会堆叠表。@David Ah,我认为你是对的。相应地更新了我的回复!
\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}
\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}