Flextable 关联的条件格式 Table
Conditional Formatting of Flextable Correlation Table
我正在寻找一种在 flextable
中对相关矩阵进行条件格式化的方法,以便在给定特定值(例如 .5)时突出显示高于该值的单元格。我查看了以前关于 SO 的条件格式问题,但未能实施解决方案。这是我的代表,最终突出显示所有单元格:
my_data <- mtcars[, c(1,3,4,5,6,7)] #data
res <- cor(my_data) #initial correlation matrix
res[upper.tri(res)] <- NA # erase the upper triangle
diag(res) <- NA
res %>%
as.data.frame() %>%
rownames_to_column("var") %>%
flextable::flextable() %>%
flextable::bg(i = rownames(res) > .5, j = 2:ncol(res), bg = "light blue")
这是一个可能的解决方案:
library(flextable)
library(magrittr)
library(tibble)
my_data <- mtcars[, c(1,3,4,5,6,7)] #data
res <- cor(my_data) #initial correlation matrix
res[upper.tri(res)] <- NA # erase the upper triangle
diag(res) <- NA
res %>%
as.data.frame() %>%
rownames_to_column("var") %>%
flextable::flextable() %>%
flextable::bg(j = 2:ncol(res),
bg = function(x){
out <- rep("transparent", length(x))
out[x < .5] <- "light blue"
out
})
我正在寻找一种在 flextable
中对相关矩阵进行条件格式化的方法,以便在给定特定值(例如 .5)时突出显示高于该值的单元格。我查看了以前关于 SO 的条件格式问题,但未能实施解决方案。这是我的代表,最终突出显示所有单元格:
my_data <- mtcars[, c(1,3,4,5,6,7)] #data
res <- cor(my_data) #initial correlation matrix
res[upper.tri(res)] <- NA # erase the upper triangle
diag(res) <- NA
res %>%
as.data.frame() %>%
rownames_to_column("var") %>%
flextable::flextable() %>%
flextable::bg(i = rownames(res) > .5, j = 2:ncol(res), bg = "light blue")
这是一个可能的解决方案:
library(flextable)
library(magrittr)
library(tibble)
my_data <- mtcars[, c(1,3,4,5,6,7)] #data
res <- cor(my_data) #initial correlation matrix
res[upper.tri(res)] <- NA # erase the upper triangle
diag(res) <- NA
res %>%
as.data.frame() %>%
rownames_to_column("var") %>%
flextable::flextable() %>%
flextable::bg(j = 2:ncol(res),
bg = function(x){
out <- rep("transparent", length(x))
out[x < .5] <- "light blue"
out
})