【问题标题】:xtable for conditional cell formatting significant p-values of tablextable 用于条件单元格格式化表格的重要 p 值
【发布时间】:2014-08-24 14:13:39
【问题描述】:

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

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

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 格式:

xtable(data.hsd.result)

在 Latex 中,输出如下所示:

                         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 值

【问题讨论】:

    标签: r formatting latex conditional xtable


    【解决方案1】:

    你好试试这个:

    \documentclass{article}
    \usepackage{color}
    \begin{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

    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}
    

    【讨论】:

    • 感谢@Victorp,但我注意到转换后的数据的小数位数要长得多。关于如何使其与其他列保持相同位数的任何提示?
    • @Victorp df$V2 &lt; 0.5 ;)
    • @PaoloCrosetto,你可以在ifelse 中做round(df$V2, 4L)。您也可以添加options(scipen = 10) 来惩罚科学记数法。
    • 如果我想强加更多的条件,例如:df$V3 = ifelse(df$V2 &lt; 0.5, paste0("\\colorbox{red}{", df$V2, "}"), df$V2)df$V3 = ifelse(df$V2 &gt;= 0.5 &amp; df$V2 &lt;0.8, paste0("\\colorbox{blue}{", df$V2, "}"), df$V2),第二行将不起作用,因为第一行代码将 df$V3 转换为“字符”,哪些数值条件不能强加。有什么办法解决这个问题?
    • @Jason Goal 你可以嵌套你的ifelse。请参阅我的编辑以获得更优雅的解决方案。
    【解决方案2】:

    Victorp 提供了一个出色的解决方案,它让我从长达数小时的挣扎中解脱出来。然后那天晚些时候我需要对同一个数据集施加多个条件,这意味着我需要根据不同条件在单元格上使用两种不同的颜色来解决这个问题,完全基于 Victorp 的回答,我想出了一个解决方案,希望这对那些人有所帮助以后需要这个。

        <<echo=FALSE, results='asis'>>=
        df = data.frame(V1 = LETTERS[1:6], V2 = runif(6, 0, 1),V3 = runif(6, 0, 1))
        ## replicate the data frame of which you are going to highlight the cells
        ## the number of duplicates should be equal to number of conditions you want to impose
        temp.1<-df
        temp.2<-df
        ## impose conditions on those temporary data frame separately.
        ## change the columns you want to 
        for (i in colnames(temp.1)[2:3]) {
        temp.1[,i]= ifelse(temp.1[,i] <= 0.5,
                                    paste0("\\colorbox{red}{", temp.1[,i], "}"), temp.1[,i])}
        rm(i)
    
    
        for (i in colnames(temp.2)[2]) {
        temp.2[,i]= ifelse(temp.2[,i] > 0.5 & temp.2[,i] <=0.8,
                                    paste0("\\colorbox{blue}{", temp.2[,i], "}"),temp.2[,i])}
        rm(i)
        ## then record the position of cells under you conditions
        pos.1<-which(df[,] <=0.5,arr.ind = TRUE)
        pos.2<-which(df[,] >0.5 & df[,]<=0.8,arr.ind = TRUE)
        ## replace cells in original data frame that you want to highlight
        ## replace those values in temp which satisfy the condition imposed on temp.1
        if(length(pos.1)>0) {
          temp[pos.1]<-temp.1[pos.1]
        }
    
    
        ## replace those values in temp which satisfy the condition imposed on temp.2
        if(length(pos.2)>0) {
          temp[pos.2]<-temp.2[pos.2]
        }
        rm(temp.1,temp.2,pos.1,pos.2)
        @
    

    然后你以你喜欢的方式打印df。这可行,但是,鉴于 R 的力量,我相信应该有更简单的方法。

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多