GT table 3 colour conditional format base on specific number

Hi everyone,

I'm trying to replicate a Excel report in RMarkdown. I have a lot already in place but I need help with conditional formatting

I have a GT (package) table that has 3 colour (White, Orange, Red) conditional formatting, but I need to tell the function to change colour at a certain number. In Excel the rule is setup like

  • Minimum (White, value = 0),
  • Midpoint(Orange, value = 50),
  • Maximum(Red, 100)

Is it possible to pass the 0, 50, 100 value to the data_color() function somehow?

Have a look on conditional formatting with kable / kableExtra https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

Regards,
Grzegorz

However, just looking on gt documentation, it seems

[...]
%>%
  tab_style(
    style = cell_fill(color = "White"),
    locations = cells_body(
      columns = currency,
      rows = currency = 0
    ) %>%
  tab_style(
    style = cell_fill(color = "Orange"),
    locations = cells_body(
      columns = currency,
      rows = currency = 50
    )
[...]

Not tested, but you got the idea.

Regards,
Grzegorz

1 Like

Here's a great blog on column coloring with GT

https://www.allisonhorst.com/post/2020-03-02-gt-tables-examples/


data_color( 
    columns = vars(YOUR_VARIABLE), 
    colors = scales::col_numeric( 
      palette = c('white', 'orange', 'red'),
      domain = c(0, 100) 
  )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.