xtable 用于条件单元格格式化表格的重要 p 值 [英] xtable for conditional cell formatting significant p-values of table

查看:36
本文介绍了xtable 用于条件单元格格式化表格的重要 p 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xtable 生成要放入 Latex 的表格,并且想知道是否有办法对单元格进行条件格式化,以便所有重要的 p 值都显示为灰色?我在 TexShop 中使用 Knitr.

I'm using xtable to generate tables to put in Latex, and was wondering if there's a way to have conditional formatting of cells so that all significant p-values are in grey? I'm using Knitr in TexShop.

这是一个使用 ggplot2 中的 diamonds 数据并运行 TukeyHSD 测试以从 cut 预测 carat 的示例.

Here's an example using the diamonds data in ggplot2, and running a TukeyHSD test to predict carat from cut.

library(ggplot2)
library(xtable)
summary(data.aov <- aov(carat~cut, data = diamonds))
data.hsd<-TukeyHSD(data.aov)
data.hsd.result<-data.frame(data.hsd$cut)
data.hsd.result

然后我可以将 data.hsd.result 转换为 xtable 格式:

I can then get data.hsd.result into xtable format with:

xtable(data.hsd.result)

在 Latex 中,输出如下所示:

In Latex, the output looks like this:

                         diff         lwr         upr        p.adj
Good-Fair         -0.19695197 -0.23342631 -0.16047764 0.000000e+00
Very Good-Fair    -0.23975525 -0.27344709 -0.20606342 0.000000e+00
Premium-Fair      -0.15418175 -0.18762721 -0.12073628 0.000000e+00
Ideal-Fair        -0.34329965 -0.37610961 -0.31048970 0.000000e+00
Very Good-Good    -0.04280328 -0.06430194 -0.02130461 5.585171e-07
Premium-Good       0.04277023  0.02165976  0.06388070 3.256208e-07
Ideal-Good        -0.14634768 -0.16643613 -0.12625923 0.000000e+00
Premium-Very Good  0.08557350  0.06974902  0.10139799 0.000000e+00
Ideal-Very Good   -0.10354440 -0.11797729 -0.08911151 0.000000e+00
Ideal-Premium     -0.18911791 -0.20296592 -0.17526989 0.000000e+00

可以有任何 p 值 <0.05 自动具有灰色背景或以某种方式突出显示?显然,对于这个集合,它将是整个列,但我希望有一些适用于我所有数据的东西.

It it possible to have any p-values < 0.05 to have a grey coloured background automatically or highlighted in some way? Obviously, for this set it would be the whole column, but I'm hoping for something that works with all my data.

推荐答案

你好试试这个:

documentclass{article}
usepackage{color}
egin{document}

<<echo=FALSE, results='asis'>>=
df = data.frame(V1 = LETTERS[1:6], V2 = runif(6, 0, 1))
df$V3 = ifelse(df$V2 < 0.5, paste0("\colorbox{red}{", df$V2, "}"), df$V2)
library(xtable)
print(xtable(df), sanitize.text.function = function(x) x)
@

end{document}

编辑

如果您有多个条件,一种解决方案是使用包 dplyr 和函数 case_when :

If you have multiple conditions, one solution is to use package dplyr and function case_when :

set.seed(123)
df <- data.frame(V1 = LETTERS[1:6], V2 = runif(6, 0, 1))

library("dplyr")
df %>% 
  mutate(
    V3 = case_when(
      V2 < 0.5 ~ paste0("\colorbox{red}{", round(V2, 3), "}"),
      V2 >= 0.5 & V2 < 0.8 ~ paste0("\colorbox{blue}{", round(V2, 3), "}"),
      TRUE ~ formatC(V2, digits = 3)
    )
  )
#   V1        V2                      V3
# 1  A 0.2875775  \colorbox{red}{0.288}
# 2  B 0.7883051 \colorbox{blue}{0.788}
# 3  C 0.4089769  \colorbox{red}{0.409}
# 4  D 0.8830174                   0.883
# 5  E 0.9404673                    0.94
# 6  F 0.0455565  \colorbox{red}{0.046}

这篇关于xtable 用于条件单元格格式化表格的重要 p 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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