在knitr / Sweave关系中着色/突出显示R ftable()输出的值 [英] Colorize/highlight values of R ftable() output in knitr/Sweave rapports

查看:211
本文介绍了在knitr / Sweave关系中着色/突出显示R ftable()输出的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会为描述性报表生成大量 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
次级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
三级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+初级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
次级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
三级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+主要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
次要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
三级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()输出和偏离行平均值的highligh值,渐变到值的文本,例如从0-100%,值从红色变为绿色。



输出现在通过 knitr ,但我不确定在工具链的哪一点,我可以介入,并添加基于值的相对大小的颜色。

latex 函数,在 Hmisc package。

 #示例无耻地从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&
cellTex [2,2] < - cellcolor {red}
cellTex [2,nrow(d)* ncol(d) 3]< - color {red}
cellTex [5,1]< - rowcolor {yellow}
latex(d,file =,cellTexCmds = cellTex,rowname = NULL )
@
\\end {document}',
file =tmp.Rnw)
Sweave(tmp.Rnw)
库utils)
texi2pdf(tmp.tex)


I am generating a lot of ftable() crosstabulations for a descriptive report. Example:

              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

I am looking for a function that could take the ftable() or table() output, and highligh values that deviate from the row-mean, or assign an overall gradient to the text of the values, e.g. from 0-100% the values are coloured from red to green.

The output is now processed through knitr, but I'm not sure at which point in the toolchain I could intervene and add colour based on the relative size of the values.

解决方案

You can use the latex function, in the Hmisc package.

# 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")

这篇关于在knitr / Sweave关系中着色/突出显示R ftable()输出的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆