【问题标题】:R tableGrob heatmap or conditional formating in columnR tableGrob 热图或列中的条件格式
【发布时间】:2018-04-27 08:53:25
【问题描述】:

有没有办法创建类似于 excel 的条件格式 -> 色阶的效果,以便在 grid.table/tablegrob 对象中显示表格?对于列中的较低值,颜色指示器应为红色,为较高值应为绿色。 需要该对象格式,以便表格可以与绘图一起以网格格式显示。

谢谢。

【问题讨论】:

    标签: r datagrid heatmap conditional-formatting grob


    【解决方案1】:

    您可以在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 
    # 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)
    

    【讨论】:

      【解决方案2】:

      您可以为此使用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 表格。然后将主题为RAG 的颜色等级应用到表格的第二列:

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

      结果如下:

      【讨论】:

      • 感谢您的意见。你知道 html 是否可以在网格图形中与 ggplots 一起用于以 pdf 格式报告(都在同一页面中)?
      • 这取决于转换引擎。最简单的解决方案是使用 webshot 包来创建 HTML 的屏幕截图。您可以使用write_tableHTML() 保存它
      【解决方案3】:

      最自然的解决方案是使用heatmap()?

      heatmap(data.matrix(mtcars))
      

      会生成带有一些默认颜色选项的热图。您可以使用附加参数(例如col = cm.colors(256))或您自己的调色板来更改颜色以实现所需的输出。

      ,

      【讨论】:

        【解决方案4】:

        我找到的一个解决方案是执行以下操作.. 这仅在数据有序并且您列出行数(根据您的屏幕截图为 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]) 
        

        希望对您有所帮助。我自己也在寻找替代品

        【讨论】:

          猜你喜欢
          • 2018-10-21
          • 2019-03-05
          • 2021-10-04
          • 2012-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多