In sweave, xxtable, only rotate column names

Suppose I want to make a table in sweave, for example:

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

enter image description here

Now I would like to have a plot shortcut in free space over the years and to the left of FishBird, but without rotation. I tried searching in the xtable manual, but it does not show how to rotate column names as soon as.

+4
source share
3 answers

Here is a workaround. At first, I put the years in a column and defined my own function for managing column names. This allows me to replace the name of the first column (in my code example here:) rotated[1]with something else.

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}

Please note that you can still have your own names. 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="&")))} 
)
+4

( 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

+1

Another option, if you do not want to deal with LaTeX code, is to use pixiedustwhich allows you to choose which columns will be changed.

\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}
0
source

Source: https://habr.com/ru/post/1676132/


All Articles