R tableGrob heatmap or conditional formating in column

↘锁芯ラ 提交于 2019-12-02 07:29:18

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)

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:

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.

,

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!