简体   繁体   English

在sweave,xtable中,只旋转一些列名

[英]In sweave, xtable, only rotate some column names

Lets say i want to make a table in sweave, like this: 让我们说我想在s​​weave中制作一张桌子,像这样:

<<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)
@

在此输入图像描述

Now, i would like to have the label of the plot in the free space above the years and left of the FishBird, but without rotation. 现在,我想在FishBird的年代和左边的自由空间中拥有情节的标签,但没有旋转。 I tried looking in the xtable manual, but it doesnt show how to only rotate some column names. 我尝试查看xtable手册,但它没有显示如何只旋转一些列名。

Here is a workaround. 这是一个解决方法。 I first put the years into a column, and define my own function to manipulate the column names. 我首先将这些年份放入一列,并定义我自己的函数来操作列名。 This allows me to replace the first column name (in my code example here: rotated[1] ) with something else. 这允许我用其他东西替换第一列名称(在我的代码示例中: 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}

Note that you can still have your rownames. 请注意,您仍然可以使用您的rownames。 The following works just as well: 以下也适用:

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="&")))} 
)

Another possibility (using my own huxtable package): 另一种可能性(使用我自己的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

在此输入图像描述

Another option, if you don't want to deal with LaTeX code directly is to use pixiedust , which lets you choose which columns will be modified. 另一个选择,如果你不想直接处理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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM