【问题标题】:Colorize/highlight values of R ftable() output in knitr/Sweave rapports在 knitr/Sweave rapports 中着色/突出显示 R ftable() 输出的值
【发布时间】:2012-03-07 19:59:20
【问题描述】:

我正在为描述性报告生成大量 ftable() 交叉表。示例:

              AUS  BEL  BUL  EST  FRA  GEO  GER  HUN  ITA  NET  NOR  ROM  RUS

30- primary    0.06 0.03 0.07 0.03 0.02 0.03 0.03 0.02 0.05 0.03 0.05 0.04 0.02
    secondary  0.30 0.09 0.16 0.10 0.10 0.14 0.10 0.16 0.11 0.08 0.08 0.09 0.11 
    tertiary   0.05 0.07 0.04 0.05 0.07 0.06 0.02 0.04 0.02 0.05 0.06 0.02 0.09
30+ primary    0.07 0.16 0.12 0.07 0.16 0.03 0.05 0.11 0.35 0.21 0.09 0.17 0.03
    secondary  0.40 0.20 0.30 0.29 0.25 0.35 0.35 0.34 0.27 0.20 0.27 0.34 0.26
    tertiary   0.13 0.23 0.13 0.18 0.17 0.17 0.18 0.09 0.09 0.23 0.23 0.06 0.24
60+ primary    0.00 0.12 0.10 0.13 0.14 0.07 0.05 0.12 0.09 0.11 0.06 0.19 0.12
    secondary  0.00 0.05 0.05 0.08 0.06 0.10 0.14 0.09 0.02 0.04 0.11 0.07 0.06
    tertiary   0.00 0.05 0.03 0.06 0.03 0.04 0.07 0.03 0.01 0.05 0.06 0.02 0.07

我正在寻找一个函数,它可以采用 ftable()table() 输出,以及偏离行均值的高亮值,或者为值的文本分配整体梯度,例如从 0-100% 的值从红色变为绿色。

现在通过knitr 处理输出,但我不确定我可以在工具链中的哪个点进行干预并根据值的相对大小添加颜色。

【问题讨论】:

  • 如果您在这里没有得到很好的回应,您也可以在 TeX/LaTeX 交换网站上发布这个问题。

标签: r colors latex knitr


【解决方案1】:

要从 R 对象生成乳胶表,您可以使用 xtable 包。它是available on CRAN,请查看文档。要获得表格中的颜色,请使用color latex 包。一些示例代码:

library(xtable)
n = 100
cat_country = c("NL","BE","HU")
cat_prim = c("primary","secondary","tertiary")
dat = data.frame(country = sample(cat_country, n, replace = TRUE), 
                 prim = sample(cat_prim, n, replace = TRUE))
ftable_dat = ftable(dat)

## Make latex table:
latex_table = xtable(as.table(ftable_dat))

为了得到你想要的东西,我做了以下 hack(丑陋的一个)。诀窍是打印 xtable 对象,然后对其进行编辑:

latex_table = within(latex_table, {
#   browser()
  primary = ifelse(primary > 12, sprintf("\\textbf{%s}", primary), primary)
  #primary = sub("\\{", "{", primary)
})
printed_table = print(latex_table)
printed_table = sub("backslash", "\\", printed_table)
printed_table = sub("\\\\}", "}", printed_table)
printed_table = sub("\\\\\\{", "{", printed_table)
printed_table = sub("\\$", "\\", printed_table)
printed_table = sub("\\$", "\\", printed_table)
cat(printed_table)

这导致:

% latex table generated in R 2.14.1 by xtable 1.6-0 package
% Thu Feb 16 13:10:55 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & primary & secondary & tertiary \\ 
  \hline
BE & 10 &   5 &  11 \\ 
  HU & \textbf{13} &  13 &   8 \\ 
  NL & 11 &  17 &  12 \\ 
   \hline

\end{表格} \结束{中心} \结束{表格}

此示例将主要类别中的数字设为粗体,但它可以同样轻松地用于着色。也许其他人有更优雅的解决方案?

【讨论】:

    【解决方案2】:

    您可以使用Hmisc 包中的latex 函数。

    # Example shamelessly copied from http://www.karlin.mff.cuni.cz/~kulich/vyuka/Rdoc/harrell-R-latex.pdf
    cat('
      \\documentclass{article}
      \\usepackage[table]{xcolor}
      \\begin{document}
      <<results=tex>>=
      library(Hmisc)
      d <- head(iris)
      cellTex <- matrix(rep("", nrow(d) * ncol(d)), nrow=nrow(d))
      cellTex[2,2] <- "cellcolor{red}"
      cellTex[2,3] <- "color{red}"
      cellTex[5,1] <- "rowcolor{yellow}"
      latex(d, file = "", cellTexCmds = cellTex, rowname=NULL)
      @
      \\end{document}',
      file="tmp.Rnw" )
    Sweave("tmp.Rnw")
    library(utils)
    texi2pdf("tmp.tex")
    

    【讨论】:

    • 1+ 用于 Hmisc::latex 示例。鉴于我从你的网页中获得的有用内容的数量,文森特,我认为你可以免费通过任何无耻的抄袭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2019-04-28
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多