R语言 在没有列名的gt表中对p值进行颜色编码

cnwbcb6i  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(140)

我有下面的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 = "")

有没有一种方法可以将小于0.05蓝色和小于0.01红色的p值进行颜色编码?我尝试过data_colors参数,但由于没有列名,它不起作用。

py49o6xq

py49o6xq1#

您使用data_color的方法是正确的。当然,您是否有列名,即使它们并不是真正需要的,因为我们只需要排除第一列:

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

相关问题