【问题标题】:In sweave, xtable, only rotate some column names在sweave、xtable中,只旋转一些列名
【发布时间】:2017-05-02 09:07:36
【问题描述】:

假设我想在 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 手册,但它没有显示如何仅旋转某些列名。

【问题讨论】:

    标签: r rotation xtable


    【解决方案1】:

    这是一种解决方法。我首先将年份放入一列,然后定义自己的函数来操作列名。这允许我用其他名称替换第一列名称(在我的代码示例中: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="&")))} 
    )
    

    【讨论】:

    • 这行得通,但保留 row.names 会很好,因为我在我的真实数据中使用了这些。 +1,但我会等待接受,看看是否有其他解决方案可用 row.names :)
    • 好吧,如果问题是您不想更改数据框,只需在传递给xtable 时将其包装到tibble::rownames_to_column 中。我已经编辑了我的解决方案,向您展示如何操作。
    【解决方案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
    

    【讨论】:

      【解决方案3】:

      如果您不想直接处理 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}
      

      【讨论】:

      • 我得到“评估错误(exp,envir,enclos):找不到对象'tex'。
      • 你用的是什么文件?我在 R Studio 中使用.Rnw 文件。
      • 是的,我正在使用相同的。我猜我错过了 Tex(在我的 PATH 中?)
      • 奇怪,当我使用results = "asis"(如您的示例)时,我收到错误Error in match.arg(options$results, c("verbatim", "tex", "hide")) : 'arg' should be one of "verbatim", "tex", "hide"尝试使用"asis",看看是否适合您。
      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多