如何在忽略字符向量的同时为列的数字单元格着色表示来自 R 的 gt 包中的 NA
How to color columns' numeric cells while ignoring character vectors represents NA in gt package from R
一个虚拟数据如下:
df <- structure(list(id = 1:4, v1 = c("/", "0.2", "0.3", "0.4"), v2 = c(0.8,
0.2, 0.1, 0.5), change = c("/", "0", "-0.2", "0.1")), class = "data.frame", row.names = c(NA,
-4L))
使用下面的代码,我可以使用 R 中的 gt
包轻松地为 v2
着色,因为它的数据类型是 numeric
:
library(tidyverse)
library(gt)
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
输出:
现在我也想给 v1
上色(请注意此列的 dtype 是 char
)并用 /
显示 NA
(因为输出数字将在报告中使用,所以我希望 \
或 -
作为符号来表示 NA
或 NaN
而不是使用 R 的默认 NA 符号。
所以我的问题是可以这样做吗?如果是,如何?感谢您的提前帮助。
编辑:
library(tidyverse)
library(gt)
library(paletteer)
na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material")
na_palette[1] <- "#FFFFFFFF" # replace "/" with white
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
) %>%
data_color(
columns = "v1",
colors = scales::col_factor(
na.color = "white",
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)%>%
data_color(
columns = "change",
colors = scales::col_factor(
na.color = "white",
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)
输出:
您可以向您的管道添加另一个 gt::data_color
调用:
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)%>%
data_color(
columns = "v1",
colors = scales::col_factor(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
在这种情况下,您只需将 scales::col_numeric
更改为 scales::col_factor
,这会为您的字符值设置颜色
编辑: 如果要将 /
设置为白色:
na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material") # get the palette
na_palette[1] <- "#FFFFFFFF" # replace "/" with white
df %>%
dplyr::mutate(
change = factor(change, levels = change),
v1 = factor(v1, levels = v1)
) %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
) %>%
data_color(
columns = c("v1", "change"),
colors = scales::col_factor(
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)
输出:
一个虚拟数据如下:
df <- structure(list(id = 1:4, v1 = c("/", "0.2", "0.3", "0.4"), v2 = c(0.8,
0.2, 0.1, 0.5), change = c("/", "0", "-0.2", "0.1")), class = "data.frame", row.names = c(NA,
-4L))
使用下面的代码,我可以使用 R 中的 gt
包轻松地为 v2
着色,因为它的数据类型是 numeric
:
library(tidyverse)
library(gt)
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
输出:
现在我也想给 v1
上色(请注意此列的 dtype 是 char
)并用 /
显示 NA
(因为输出数字将在报告中使用,所以我希望 \
或 -
作为符号来表示 NA
或 NaN
而不是使用 R 的默认 NA 符号。
所以我的问题是可以这样做吗?如果是,如何?感谢您的提前帮助。
编辑:
library(tidyverse)
library(gt)
library(paletteer)
na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material")
na_palette[1] <- "#FFFFFFFF" # replace "/" with white
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
) %>%
data_color(
columns = "v1",
colors = scales::col_factor(
na.color = "white",
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)%>%
data_color(
columns = "change",
colors = scales::col_factor(
na.color = "white",
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)
输出:
您可以向您的管道添加另一个 gt::data_color
调用:
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)%>%
data_color(
columns = "v1",
colors = scales::col_factor(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
在这种情况下,您只需将 scales::col_numeric
更改为 scales::col_factor
,这会为您的字符值设置颜色
编辑: 如果要将 /
设置为白色:
na_palette <- paletteer::paletteer_d(palette = "ggsci::red_material") # get the palette
na_palette[1] <- "#FFFFFFFF" # replace "/" with white
df %>%
dplyr::mutate(
change = factor(change, levels = change),
v1 = factor(v1, levels = v1)
) %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
) %>%
data_color(
columns = c("v1", "change"),
colors = scales::col_factor(
palette = na_palette %>% as.character(),#use the palette
domain = NULL
)
)
输出: