R tableGrob heatmap or conditional formating in co

2019-03-04 00:15发布

Is there a way to create a similar effect to excel's conditional formating -> color scales in order to present a table in grid.table/tablegrob object? The color indicator should be red for the lower values and green for the higher values in the column. That object format is needed so the table can be presented in grid format along with plots.

enter image description here

Thank you.

4条回答
forever°为你锁心
2楼-- · 2019-03-04 00:39

A solution I found was to do the following.. this only works if the data is in order and you list the count of rows(17 based on your screenshot):

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]) 

Hope that helps. I am also seeking for alternatives myself

查看更多
ら.Afraid
3楼-- · 2019-03-04 00:44

You could use tableHTML for that:

library(tableHTML)

for the dataset:

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

You can than create an HTML table using the tableHTML() function. Then apply a colour rank with theme RAG to the 2nd column of the table:

my_data %>% 
  tableHTML(rownames = FALSE,
            widths = c(50, 100)) %>% 
  add_css_conditional_column(columns = 2,
                             colour_rank_theme = 'RAG',
                             decreasing = TRUE)

The result looks like this:

enter image description here

查看更多
对你真心纯属浪费
4楼-- · 2019-03-04 00:48

The most natural solution for that is to use a heatmap()?

heatmap(data.matrix(mtcars))

Would yield a heatmap with some default color options. You can change the color using an additional parameter (e.g col = cm.colors(256)) or your own color palette to achieve the desired output.

enter image description here,

查看更多
smile是对你的礼貌
5楼-- · 2019-03-04 00:58

You can do this within tableGrob. You create a vector of colours, and then assign these to the cells.

So using the data from clemens's answer:

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 
# https://stackoverflow.com/questions/34517031/red-amber-green-sequential-palette-for-treemap-in-r
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
# https://stackoverflow.com/questions/31506294/gtable-put-a-black-line-around-all-cells-in-the-table-body
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)
查看更多
登录 后发表回答