英文:
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 = "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:
https://stackoverflow.com/questions/73599951/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?
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 < 0 | is.na(x), f_neg(x), f_pos(x))
答案1
得分: 2
你有两个调色板,pal
和 corr_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)
<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) ~ "transparent"` to the `case_when`.
- In `corr_palette_pos`, specify `na.color = "transparent"`. 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论