使NA在使用调色板函数时变为透明。

huangapple go评论104阅读模式
英文:

Make NA transparent when using a color palette-function

问题

这是我的数据框架:

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)

但我希望它是透明的或白色,就像背景一样。我找到了很多示例:Stack Overflow链接

你可以尝试在 data_color 函数中设置 na.color 参数,将 NA 值颜色设置为透明。像这样:

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)

这应该使 NA 值变成透明。如果所有值都以相同颜色显示,你可能需要检查一下是否已正确指定列名和颜色函数。

英文:

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 = &quot;data.frame&quot;)

I defined a color-palette, one for column Delta_p and one for Corr_p

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

library(scales)
corr_palette_pos &lt;- col_numeric(c(&quot;#FEF0D9&quot;, &quot;#990000&quot;), 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;%
  gt() %&gt;% 
  data_color(columns = c(Delta_p), colors = pal) %&gt;%
  data_color(columns = c(Corr_p), colors = corr_palette_pos) 

使NA在使用调色板函数时变为透明。

But I want it to be transparent or white, just like the backround. I found so many examples:
https://stackoverflow.com/questions/73599951/r-gt-table-make-na-values-not-appear

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

But when I use it like:

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

All of the values are in the same color:

使NA在使用调色板函数时变为透明。

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?

https://stackoverflow.com/questions/64469714/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 &lt; 0 | is.na(x), f_neg(x), f_pos(x))

答案1

得分: 2

你有两个调色板,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) 

使NA在使用调色板函数时变为透明。


<details>
<summary>英文:</summary>
You have two palettes, `pal` and `corr_palette_pos`. In both, you need to specify how to handle `NA`s.
- In `pal`, add `is.na(x) ~ &quot;transparent&quot;` to the `case_when`.
- In `corr_palette_pos`, specify `na.color = &quot;transparent&quot;`. Note that you can use `domain = NULL` to have the domain inferred from the data.

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)

[![image][1]][1]
[1]: https://i.stack.imgur.com/JHMFq.png
</details>

huangapple
  • 本文由 发表于 2023年3月12日 11:11:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定