在Sweave和xtable中,如何仅旋转某些列名?

5
假设我想在Sweave中制作一个表格,就像这样:
<<tab2.1 , results = "asis", echo=FALSE, warning=FALSE>>=
library(xtable)
df <- data.frame(Fish=1:5, Bird=11:15)

rownames(df) <- 2013:2017

print(xtable(df),
      rotate.colnames = TRUE)
@

在此输入图片描述

现在,我想要将图表标签放在年份上方和FishBird左侧的空白处,但不旋转。我尝试查看xtable手册,但它没有显示如何仅旋转某些列名。

3个回答

4

这里有一个解决方法。首先将年份放入一列,并定义自己的函数来操作列名。这样可以用其他内容替换第一列的名称(在我的代码示例中是rotated[1])。

library(xtable)
df <- data.frame(rows = 2013:2017, Fish=1:5, Bird=11:15)
# note that the rownames have their own column

print(xtable(df), include.rownames = F, 
      sanitize.colnames.function = function(x){
        rotated <- paste("\\begin{sideways}", x, "\\end{sideways}") 
        # put all column names into sideways environments for the rotation.
        return(c("Need coffee!", paste(rotated[-1], collapse="&")))} 
        # replaces first column name with something else (not rotated).
)

\begin{table}[ht]
\centering
\begin{tabular}{rrr}
  \hline
Need coffee! & \begin{sideways} Fish \end{sideways} &\begin{sideways} Bird \end{sideways} \\ 
  \hline
2013 &   1 &  11 \\ 
  2014 &   2 &  12 \\ 
  2015 &   3 &  13 \\ 
  2016 &   4 &  14 \\ 
  2017 &   5 &  15 \\ 
   \hline
\end{tabular}
\end{table}

请注意,您仍然可以使用行名。以下内容同样适用:

请注意,您仍然可以使用行名。以下内容同样适用:

df <- data.frame(Fish=1:5, Bird=11:15)
rownames(df) <- 2013:2017
print(xtable(tibble::rownames_to_column(df)), include.rownames = F, 
      sanitize.colnames.function = function(x){
        rotated <- paste("\\begin{sideways}", x, "\\end{sideways}") 
        return(c("Need coffee!", paste(rotated[-1], collapse="&")))} 
)

这个方法可行,但最好保留行名,因为我在实际数据中使用了它们。+1,不过我会等待其他解决方案,看是否可以使用行名 :) - Jeppe Olsen
如果问题是您不想更改数据框,则只需在传递给xtable时将其包装到tibble::rownames_to_column中。我已经编辑了我的解决方案以向您展示如何操作。 - coffeinjunky

2

另一种可能性(使用我自己的huxtable包):

library(huxtable)
df <- data.frame(Fish=1:5, Bird=11:15)
rownames(df) <- 2013:2017
ht <- hux(df, add_rownames = TRUE, add_colnames = TRUE)
ht[1, 1] <- 'Your advert here'
number_format(ht) <- 0
rotation(ht)[1, 2:3] <- 90
ht

enter image description here


0
另一个选择是,如果您不想直接处理LaTeX代码,则可以使用pixiedust,它可以让您选择要修改的列。
\documentclass{article}
\usepackage{amssymb}
\usepackage{arydshln}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{hhline}
\usepackage{longtable}
\usepackage{multirow}
\usepackage[dvipsnames,table]{xcolor}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<tab2.1, results = tex>>=
df <- data.frame(Year = 2013:2017, Fish=1:5, Bird=11:15)

library(pixiedust)
options(pixiedust_print_method = "latex") # This option must be set in Rnw files

dust(df) %>%
  medley_bw() %>%
  sprinkle(cols = c("Fish", "Bird"),
           rotate_degree = 90,
           part = "head") %>%
  print() %>%
  cat()
@


\end{document}

我收到了“在 eval(exp,envir,enclos) 中的错误:找不到对象'tex'。” - Jeppe Olsen
你使用的是什么类型的文件?我在R Studio中使用了一个.Rnw文件。 - Benjamin
是的,我正在使用相同的东西。我猜我缺少了Tex(在我的路径中?) - Jeppe Olsen
当我使用results = "asis"(就像你的例子中一样)时,我会收到错误消息Error in match.arg(options$results, c("verbatim", "tex", "hide")) : 'arg' should be one of "verbatim", "tex", "hide"。请尝试使用"asis",看看是否可以为您构建。 - Benjamin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接