简体   繁体   English

没有列名的 gt 表中的颜色编码 p 值

[英]Colour coding p values in a gt table with no column names

I have the following gt table:我有以下 gt 表:

library(dplyr)
library(gt)
group1 <- c("0", "1", "10", "100", "1000")
group2 <- c("0", "1", "10", "100", "1000")
pvalue <- c("0.06", "0.042", "0.001", "1", "0.02")
data <- data.frame(group1, group2, pvalue)

data %>% 
  pivot_wider(names_from = group2,
              values_from = pvalue) %>%
  gt() %>%
  cols_label(group1 = "") %>% 
    sub_missing(
  columns = everything(),
  rows = everything(),
  missing_text = "") 

在此处输入图像描述

Is there a way to colour code p values that are less than 0.05 blue, and less than 0.01 red?有没有办法对小于 0.05 蓝色和小于 0.01 红色的 p 值进行颜色编码? I have tried the data_colours argument, but as I have no column names it isn't working.我试过 data_colours 参数,但因为我没有列名,所以它不起作用。

You were on the right track with using data_color .您使用data_color在正确的轨道上。 And of course do you have column names even if they are not really needed as we only have to exclude the first column:当然,你是否有列名,即使它们不是真正需要的,因为我们只需要排除第一列:

library(dplyr)
library(tidyr)
library(gt)

data %>% 
  pivot_wider(names_from = group2,
              values_from = pvalue) %>%
  mutate(across(-1, as.numeric)) %>%
  gt() %>%
  cols_label(group1 = "") %>% 
  sub_missing(
    columns = everything(),
    rows = everything(),
    missing_text = "") %>%
  data_color(
    columns = -1,
    colors = scales::col_bin(
      palette = c("red", "blue", "transparent"),
      bins = c(0, 0.01, .05, 1),
      na.color = "transparent"
    )
  )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM