This is my 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")
I defined a color-palette, one for column Delta_p and one for 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)
Then I plot the table. As you can see its using the default color for the NA values.
Test %>%
gt() %>%
data_color(columns = c(Delta_p), colors = pal) %>%
data_color(columns = c(Corr_p), colors = corr_palette_pos)
But I want it to be transparent or white, just like the backround. I found so many examples: 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"
)
) %>%
But when I use it like:
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)
All of the values are in the same color:
Any idea please, how to solve that problem? My thought is that I have to define that somehow in the pal-function?!
UPDATE
PS: Really nobody that could help?
Set asymmetric midpoint for data_color in gt table In this topic there is a comment on how to change the NA-Color within a if-else-Statement. I tried to implement it in the case-when-Statement, but I was not successful. I think that its the right spot to change it, but I just dont know how.
ifelse(x < 0 | is.na(x), f_neg(x), f_pos(x))