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

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

Make NA transparent when using a color palette-function

问题

这是我的数据框架:

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

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

  1. library(gt)
  2. pal <- function(x) {
  3. f_neg <- scales::col_numeric(
  4. palette = c(emerald),
  5. domain = c(min(test, na.rm = TRUE), 0)
  6. )
  7. f_pos1 <- scales::col_numeric(
  8. palette = c(oryel),
  9. domain = c(0, 300)
  10. )
  11. f_pos2 <- scales::col_numeric(
  12. palette = c(sunshort),
  13. domain = c(300, 500)
  14. )
  15. f_pos3 <- scales::col_numeric(
  16. palette = c(toplevel),
  17. domain = c(500, max(test, na.rm = TRUE))
  18. )
  19. dplyr::case_when(
  20. x < 0 ~ f_neg(x),
  21. x < 300 ~ f_pos1(x),
  22. x < 500 ~ f_pos2(x),
  23. .default = f_pos3(x)
  24. )
  25. }
  26. library(scales)
  27. corr_palette_pos <- col_numeric(c("#FEF0D9", "#990000"), domain = c(min_cor_pos, max_cor_pos), alpha = 0.75)

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

  1. Test %>%
  2. gt() %>%
  3. data_color(columns = c(Delta_p), colors = pal) %>%
  4. data_color(columns = c(Corr_p), colors = corr_palette_pos)

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

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

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

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

英文:

This is my dataframe:

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

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

  1. library(gt)
  2. pal &lt;- function(x) {
  3. f_neg &lt;- scales::col_numeric(
  4. palette = c(emerald),
  5. domain = c(min(test, na.rm = TRUE), 0)
  6. )
  7. f_pos1 &lt;- scales::col_numeric(
  8. palette = c(oryel),
  9. domain = c(0, 300)
  10. )
  11. f_pos2 &lt;- scales::col_numeric(
  12. palette = c(sunshort),
  13. domain = c(300, 500)
  14. )
  15. f_pos3 &lt;- scales::col_numeric(
  16. palette = c(toplevel),
  17. domain = c(500, max(test, na.rm = TRUE))
  18. )
  19. dplyr::case_when(
  20. x &lt; 0 ~ f_neg(x),
  21. x &lt; 300 ~ f_pos1(x),
  22. x &lt; 500 ~ f_pos2(x),
  23. .default = f_pos3(x)
  24. )
  25. }
  26. library(scales)
  27. 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.

  1. Test %&gt;%
  2. gt() %&gt;%
  3. data_color(columns = c(Delta_p), colors = pal) %&gt;%
  4. 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

  1. data_color(
  2. columns = colnames(df)[grep(&quot;res&quot;, colnames(df))],
  3. colors = scales::col_numeric(
  4. palette = pal,
  5. domain = c(0, 20),
  6. na.color = &quot;transparent&quot;
  7. )
  8. ) %&gt;%

But when I use it like:

  1. Test %&gt;%
  2. gt() %&gt;%
  3. data_color(columns = c(Delta_p), colors = scales::col_numeric(palette = pal,
  4. domain = c(29.06,97.73),
  5. na.color = &quot;transparent&quot;)) %&gt;%
  6. 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.

  1. 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 从数据中推断域。
  1. library(gt)
  2. library(scales)
  3. emerald = c("#d3f2a3","#97e196","#6cc08b","#4c9b82","#217a79","#105965","#074050")
  4. oryel = c("#ecda9a","#efc47e","#f3ad6a","#f7945d","#f97b57","#f66356","#ee4d5a")
  5. sunshort = c("#dc3977","#b9257a","#7c1d6f")
  6. toplevel = c("#6c2167", "#541f3f")
  7. Test = structure(list(Corr_p = c(0.65, 0.64, 0.64, NA, 0.79, 0.77),
  8. Case_p = c(24.57, 12.89, 20.92, NA, 11.1, 12.2),
  9. Control_p = c(16.4, 9.38, 16.21, NA, 7, 6.17),
  10. Delta_p = c(49.82, 37.42, 29.06, NA, 58.57, 97.73)),
  11. row.names = 18:23, class = "data.frame")
  12. pal <- function(x) {
  13. f_neg <- col_numeric(
  14. palette = emerald,
  15. domain = c(min(Test, na.rm = TRUE), 0)
  16. )
  17. f_pos1 <- col_numeric(
  18. palette = oryel,
  19. domain = c(0, 300)
  20. )
  21. f_pos2 <- col_numeric(
  22. palette = sunshort,
  23. domain = c(300, 500)
  24. )
  25. f_pos3 <- col_numeric(
  26. palette = toplevel,
  27. domain = c(500, max(Test, na.rm = TRUE))
  28. )
  29. dplyr::case_when(
  30. x < 0 ~ f_neg(x),
  31. x < 300 ~ f_pos1(x),
  32. x < 500 ~ f_pos2(x),
  33. is.na(x) ~ "transparent",
  34. .default = f_pos3(x)
  35. )
  36. }
  37. corr_palette_pos <- col_numeric(
  38. c("#FEF0D9", "#990000"),
  39. domain = NULL,
  40. alpha = 0.75,
  41. na.color = "transparent"
  42. )
  43. Test %>%
  44. gt() %>%
  45. data_color(columns = Delta_p, colors = pal) %>%
  46. data_color(columns = Corr_p, colors = corr_palette_pos)

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

  1. <details>
  2. <summary>英文:</summary>
  3. You have two palettes, `pal` and `corr_palette_pos`. In both, you need to specify how to handle `NA`s.
  4. - In `pal`, add `is.na(x) ~ &quot;transparent&quot;` to the `case_when`.
  5. - 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)

  1. [![image][1]][1]
  2. [1]: https://i.stack.imgur.com/JHMFq.png
  3. </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:

确定