R语言 使用调色板功能时使NA透明

dpiehjr4  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(157)

这是我的 Dataframe :

Test = structure(list(Corr_p = c(0.65, 0.64, 0.64, NA, 0.79, 0.77), 
                      Case_p = c(24.57, 12.89, 20.92, NA, 11.1, 12.2), 
                      Control_p = c(16.4, 9.38, 16.21, NA, 7, 6.17), 
                      Delta_p = c(49.82, 37.42, 29.06, NA, 58.57, 97.73)), 
                 row.names = 18:23, class = "data.frame")

我定义了一个调色板,一个用于Delta_p列,一个用于Corr_p列

library(gt)
pal <- function(x) {
  f_neg <- scales::col_numeric(
    palette = c(emerald),
    domain = c(min(test, na.rm = TRUE), 0)
  )
  f_pos1 <- scales::col_numeric(
    palette = c(oryel),
    domain = c(0, 300)
  )
  f_pos2 <- scales::col_numeric(
    palette = c(sunshort),
    domain = c(300, 500)
  )
  f_pos3 <- scales::col_numeric(
    palette = c(toplevel),
    domain = c(500, max(test, na.rm = TRUE))
  )
  
  dplyr::case_when(
    x < 0 ~ f_neg(x),
    x < 300 ~ f_pos1(x),
    x < 500 ~ f_pos2(x),
    .default = f_pos3(x)
  )
}

library(scales)
corr_palette_pos <- col_numeric(c("#FEF0D9", "#990000"), domain = c(min_cor_pos, max_cor_pos), alpha = 0.75)

然后我绘制表格。如你所见,它使用NA值的默认颜色。

Test %>%
  gt() %>% 
  data_color(columns = c(Delta_p), colors = pal) %>%
  data_color(columns = c(Corr_p), colors = corr_palette_pos)

但是我希望它是透明的或者白色的,就像背景一样,我找到了这么多的例子:R gt table make NA values not appear

data_color(
    columns = colnames(df)[grep("res", colnames(df))],
    colors = scales::col_numeric(
      palette = pal,
      domain = c(0, 20),
      na.color = "transparent"
    )
  ) %>%

但当我这样使用它时:

Test %>%
  gt() %>% 
  data_color(columns = c(Delta_p), colors = scales::col_numeric(palette = pal,
                                                                domain = c(29.06,97.73), 
                                                                na.color = "transparent")) %>%
  data_color(columns = c(Corr_p), colors = corr_palette_pos)

所有值都使用相同的颜色:

有什么想法吗,如何解决这个问题?我的想法是我必须在pal函数中定义它?!

更新

PS:真的没有人能帮忙吗?
Set asymmetric midpoint for data_color in gt table本主题中有一个关于如何在if-else-语句中更改NA颜色的注解。我尝试在case-when-语句中实现它,但没有成功。我认为这是更改它的正确位置,但我不知道如何更改。

ifelse(x < 0 | is.na(x), f_neg(x), f_pos(x))
ibps3vxo

ibps3vxo1#

您有两个调色板palcorr_palette_pos,在这两个调色板中,您都需要指定如何处理NA

  • pal中,将is.na(x) ~ "transparent"添加到case_when
  • corr_palette_pos中,指定na.color = "transparent"。注意,您可以使用domain = NULL从数据中推断域。
library(gt)
library(scales)

emerald = c("#d3f2a3","#97e196","#6cc08b","#4c9b82","#217a79","#105965","#074050")
oryel = c("#ecda9a","#efc47e","#f3ad6a","#f7945d","#f97b57","#f66356","#ee4d5a")
sunshort = c("#dc3977","#b9257a","#7c1d6f")
toplevel = c("#6c2167", "#541f3f")

Test = structure(list(Corr_p = c(0.65, 0.64, 0.64, NA, 0.79, 0.77), 
                      Case_p = c(24.57, 12.89, 20.92, NA, 11.1, 12.2), 
                      Control_p = c(16.4, 9.38, 16.21, NA, 7, 6.17), 
                      Delta_p = c(49.82, 37.42, 29.06, NA, 58.57, 97.73)), 
                 row.names = 18:23, class = "data.frame")

pal <- function(x) {
  f_neg <- col_numeric(
    palette = emerald,
    domain = c(min(Test, na.rm = TRUE), 0)
  )
  f_pos1 <- col_numeric(
    palette = oryel,
    domain = c(0, 300)
  )
  f_pos2 <- col_numeric(
    palette = sunshort,
    domain = c(300, 500)
  )
  f_pos3 <- col_numeric(
    palette = toplevel,
    domain = c(500, max(Test, na.rm = TRUE))
  )
  
  dplyr::case_when(
    x < 0 ~ f_neg(x),
    x < 300 ~ f_pos1(x),
    x < 500 ~ f_pos2(x),
    is.na(x) ~ "transparent",
    .default = f_pos3(x)
  )
}

corr_palette_pos <- col_numeric(
  c("#FEF0D9", "#990000"),
  domain = NULL,
  alpha = 0.75,
  na.color = "transparent"
)

Test %>%
  gt() %>% 
  data_color(columns = Delta_p, colors = pal) %>%
  data_color(columns = Corr_p, colors = corr_palette_pos)

相关问题