R tableGrob 热图或列中的条件格式
R tableGrob heatmap or conditional formating in column
有没有一种方法可以创建与 excel 的条件格式 -> 色阶类似的效果,以便在 grid.table/tablegrob 对象中呈现 table?颜色指示器对于列中的较低值应为红色,对于较高值应为绿色。
需要该对象格式,以便 table 可以与绘图一起以网格格式呈现。
谢谢。
最自然的解决方案是使用 heatmap()
?
heatmap(data.matrix(mtcars))
将生成带有一些默认颜色选项的热图。您可以使用附加参数(例如 col = cm.colors(256)
)或您自己的调色板更改颜色以获得所需的输出。
,
你可以使用 tableHTML
:
library(tableHTML)
对于数据集:
set.seed(666)
my_data <- data.frame(ID = 101:117,
Balance = sample(-1000:60000, 17))
ID Balance
1 101 46237
2 102 11030
3 103 58657
4 104 11280
5 105 21034
6 106 44296
7 107 58697
8 108 29381
9 109 -188
10 110 14854
11 111 46322
12 112 -2
13 113 4839
14 114 7670
15 115 11875
16 116 48475
17 117 1228
您可以使用 tableHTML()
函数创建 HTML table。然后将主题为 RAG
的颜色等级应用于 table:
的第 2 列
my_data %>%
tableHTML(rownames = FALSE,
widths = c(50, 100)) %>%
add_css_conditional_column(columns = 2,
colour_rank_theme = 'RAG',
decreasing = TRUE)
结果如下所示:
您可以在 tableGrob
内完成此操作。您创建一个颜色矢量,然后将它们分配给单元格。
所以使用 clemens 的回答中的数据:
library(gridExtra)
library(grid)
# define colour vector
# change `vec` argument of `findInterval` to suit your cut-points
cols <- c("red" ,"orange", "green") [findInterval(my_data$Balance, c(-Inf, 1e4, 2e4, Inf))]
# or
#
cols <- colorRampPalette(c("red", "yellow", "green"))(nrow(my_data))[rank(my_data$Balance)]
# create tales individually for each column
# this make it easy to assign colours to rows
t1 <- tableGrob(my_data["Balance"],
theme=ttheme_default(
core=list(bg_params = list(fill=cols)),
colhead = list(bg_params=list(fill="white", col="grey90"))),
rows = NULL)
t2 <- tableGrob(my_data["ID"],
theme=ttheme_default(
core=list(bg_params = list(fill="white", col="grey90")),
colhead = list(bg_params=list(fill="white", col="grey90"))),
rows = NULL)
# join tables
tab <- gtable_combine(t2, t1)
# grid.newpage() ; grid.draw(tab)
# if also want to add black border
#
library(gtable)
tab <- gtable::gtable_add_grob(tab,
grobs = rectGrob(gp=gpar(fill=NA, lwd=2)),
t = 1, b = nrow(tab), l = 1, r = ncol(tab))
grid.newpage() ; grid.draw(tab)
我找到的解决方案是执行以下操作。这仅在数据有序并且您列出行数(根据您的屏幕截图为 17)时有效:
theme=ttheme_default(
core=list(bg_params = list(fill=blues9[1:17]) or
theme=ttheme_default(
core=list(bg_params = list(fill=blues9[1:17])
希望对您有所帮助。我自己也在寻找替代品
有没有一种方法可以创建与 excel 的条件格式 -> 色阶类似的效果,以便在 grid.table/tablegrob 对象中呈现 table?颜色指示器对于列中的较低值应为红色,对于较高值应为绿色。 需要该对象格式,以便 table 可以与绘图一起以网格格式呈现。
谢谢。
最自然的解决方案是使用 heatmap()
?
heatmap(data.matrix(mtcars))
将生成带有一些默认颜色选项的热图。您可以使用附加参数(例如 col = cm.colors(256)
)或您自己的调色板更改颜色以获得所需的输出。
你可以使用 tableHTML
:
library(tableHTML)
对于数据集:
set.seed(666)
my_data <- data.frame(ID = 101:117,
Balance = sample(-1000:60000, 17))
ID Balance
1 101 46237
2 102 11030
3 103 58657
4 104 11280
5 105 21034
6 106 44296
7 107 58697
8 108 29381
9 109 -188
10 110 14854
11 111 46322
12 112 -2
13 113 4839
14 114 7670
15 115 11875
16 116 48475
17 117 1228
您可以使用 tableHTML()
函数创建 HTML table。然后将主题为 RAG
的颜色等级应用于 table:
my_data %>%
tableHTML(rownames = FALSE,
widths = c(50, 100)) %>%
add_css_conditional_column(columns = 2,
colour_rank_theme = 'RAG',
decreasing = TRUE)
结果如下所示:
您可以在 tableGrob
内完成此操作。您创建一个颜色矢量,然后将它们分配给单元格。
所以使用 clemens 的回答中的数据:
library(gridExtra)
library(grid)
# define colour vector
# change `vec` argument of `findInterval` to suit your cut-points
cols <- c("red" ,"orange", "green") [findInterval(my_data$Balance, c(-Inf, 1e4, 2e4, Inf))]
# or
#
cols <- colorRampPalette(c("red", "yellow", "green"))(nrow(my_data))[rank(my_data$Balance)]
# create tales individually for each column
# this make it easy to assign colours to rows
t1 <- tableGrob(my_data["Balance"],
theme=ttheme_default(
core=list(bg_params = list(fill=cols)),
colhead = list(bg_params=list(fill="white", col="grey90"))),
rows = NULL)
t2 <- tableGrob(my_data["ID"],
theme=ttheme_default(
core=list(bg_params = list(fill="white", col="grey90")),
colhead = list(bg_params=list(fill="white", col="grey90"))),
rows = NULL)
# join tables
tab <- gtable_combine(t2, t1)
# grid.newpage() ; grid.draw(tab)
# if also want to add black border
#
library(gtable)
tab <- gtable::gtable_add_grob(tab,
grobs = rectGrob(gp=gpar(fill=NA, lwd=2)),
t = 1, b = nrow(tab), l = 1, r = ncol(tab))
grid.newpage() ; grid.draw(tab)
我找到的解决方案是执行以下操作。这仅在数据有序并且您列出行数(根据您的屏幕截图为 17)时有效:
theme=ttheme_default(
core=list(bg_params = list(fill=blues9[1:17]) or
theme=ttheme_default(
core=list(bg_params = list(fill=blues9[1:17])
希望对您有所帮助。我自己也在寻找替代品