5

Lets say i want to make a table in sweave, like this:

<<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 the label of the plot in the free space above the years and left of the FishBird, but without rotation. I tried looking in the xtable manual, but it doesnt show how to only rotate some column names.

Jeppe Olsen
  • 968
  • 8
  • 19

3 Answers3

4

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.

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. 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="&")))} 
)
coffeinjunky
  • 11,254
  • 39
  • 57
  • This works, but would be nice to keep the row.names, as I am using those in my real data. +1, but I will wait with the accept to see if another solution is available using the row.names :) – Jeppe Olsen May 02 '17 at 10:49
  • Well, if the problem is that you don't want to change the dataframe, just wrap it into `tibble::rownames_to_column` when passing to `xtable`. I have edited my solution to show you how. – coffeinjunky May 02 '17 at 11:02
2

Another possibility (using my own huxtable package):

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

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.

\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}
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • I get the "Error in eval(exp, envir, enclos) : object 'tex' not found. – Jeppe Olsen May 02 '17 at 10:39
  • What kind of file are you using? I'm using a `.Rnw` file in R Studio. – Benjamin May 02 '17 at 10:41
  • Yes, i am using the same. Guess i am missing the Tex (in my PATH?) – Jeppe Olsen May 02 '17 at 10:43
  • strange, when I use `results = "asis"` (as in your example), I get the error `Error in match.arg(options$results, c("verbatim", "tex", "hide")) : 'arg' should be one of "verbatim", "tex", "hide"` Try using `"asis"` and see if that builds for you. – Benjamin May 02 '17 at 10:45