0

I have the following data :

library(data.table)
DT <- data.table(
  Pays = c("Austria", "Belgium", "Brazil", "Bulgaria", 
           "Canada", "China", "Croatia", "Cyprus", "Czechia", "Denmark"), 
  `Nouveaux cas` = c("212.2", "136.0", "143.5", "258.7", "122.8", 
                      "0.0", "615.0", "299.0", "327.7", "314.5"), 
  `Nouveaux décès` = c("53.8", "35.4", "14.9", "89.7", "14.2", "0.0", "79.0", "14.4", "47.3", "6.9"), 
  `Évolution du nombre de cas` = c("-21.5%", "2.8%", "5.6%", "-5.2%", "4.1%", "-0.9%", "5.5%", "34.3%", "36.7%", "73.8%"), 
  `Évolution du nombre de décès` = c("-20.1%", "-20.4%", "10.5%", "-8.5%", "24.1%", "NaN%", "2.9%", "63.6%", "-5.4%", "3.6%"))

> DT
        Pays Nouveaux cas Nouveaux décès Évolution du nombre de cas Évolution du nombre de décès
 1:  Austria        212.2           53.8                     -21.5%                       -20.1%
 2:  Belgium        136.0           35.4                       2.8%                       -20.4%
 3:   Brazil        143.5           14.9                       5.6%                        10.5%
 4: Bulgaria        258.7           89.7                      -5.2%                        -8.5%
 5:   Canada        122.8           14.2                       4.1%                        24.1%
 6:    China          0.0            0.0                      -0.9%                         NaN%
 7:  Croatia        615.0           79.0                       5.5%                         2.9%
 8:   Cyprus        299.0           14.4                      34.3%                        63.6%
 9:  Czechia        327.7           47.3                      36.7%                        -5.4%
10:  Denmark        314.5            6.9                      73.8%                         3.6%

I wonder the best way to get an object, to be inserted in a word file, that would be this table with one heatmap per column. I can export this table in Excel and use conditional formating to get the desired output :

enter image description here

Now I am unsure what is the best way to do that in R. My quick searches yielded me :

  • Some heatmaps in ggplot2, e.g. here but it seems mainly two dimensional, and not to account for a lot of other options such as renaming columns and so on
  • Various packages to do tables, e.g. here, but then I am a bit lost in all the choices. I explored a bit the gtoption a few weeks ago, but I do not recall there being this option, in addition to lacking a possibility to output vectorialized table (which would be ideal but not necessary)
Anthony Martin
  • 767
  • 1
  • 9
  • 28
  • 1
    clemens answer [here](https://stackoverflow.com/questions/50058750/r-tablegrob-heatmap-or-conditional-formating-in-column) may be useful – user20650 Dec 16 '20 at 16:32

1 Answers1

4

Here's an approach with gt.

gt::data_color will only really work for numeric data if it is actually numeric, so I converted the percent columns in your data back to numeric. I used readr::parse_number because it works so easily, but you could do it manually if you want.

scales::col_numeric makes it easy to define the color scales. Change the domain = argument to modify the range.

library(gt)
library(scales)
library(readr)
DT <- DT[,lapply(.SD,readr::parse_number),by = Pays]
DT %>%
  gt() %>%
  data_color(columns = 2:3, 
             colors = col_numeric(palette = c("white","red"),
                                  domain = c(0,650))) %>%
  data_color(columns = 4:5, 
             colors = col_numeric(palette = c("green","yellow","red"),
                                  domain = c(-30,80))) %>%
  fmt_percent(columns = 4:5, scale_values = FALSE, decimals = 1)

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57