Color rows gt table under condition of commun value

Hello,

I'd like to know if you can help me,

# Dataframe example

Species_name <- c("Cryptocephalus aureolus"    ,
                  "Cryptocephalus	aureolus"  ,
                  "Cryptocephalos draconica"   ,
                  "Test testi"                 ,
                  "Cryptocephalus aureoli"     ,
                  "Cryptocephalus	pulsillus"
                  )

First_descriptor <- c("Seguy"       ,
                      "Moribondi"   ,
                      "Valder"      ,
                      "Moribondi"   ,
                       "Valder"     ,
                       "Roque"
)

Is_valid <- c(    "TRUE"   ,
                  "FALSE"  ,
                  "TRUE"   ,
                  "TRUE"   ,
                  "TRUE"   ,
                  "FALSE"
)

df <- data.frame(Species_name, First_descriptor , Is_valid)

#  Colouring rows under a condition using gt table library

library(gt) 
library(dplyr)



df %>%
  
  gt() %>%
  
  
  cols_align(
    align = c("left"),
    columns = everything()
  ) %>%
  
  tab_style(
    style = cell_fill(color = "#9AFF9A"), 
    locations = cells_body(
      rows = Is_valid == "FALSE") 
  )


I'm looking for a way to colour rows with the same colour according to the sharing of a qualitative value for a given column.

A simpler example works well when the column only contains two values, TRUE or FALSE, for example. However, when the number of possible pairs of values increases I can't figure out how to go about it.

Could you please enlighten me?

Thanks a lot

Hi @Thibaud,

something along these lines (?) It still keeps the expressions of dplyr intact - but you still have to take care to check things (enought colors are provided etc.) .

Full code:

# Dataframe example

Species_name <- c(
  "Cryptocephalus aureolus"    ,
  "Cryptocephalus	aureolus"  ,
  "Cryptocephalos draconica"   ,
  "Test testi"                 ,
  "Cryptocephalus aureoli"     ,
  "Cryptocephalus	pulsillus"
)

First_descriptor <- c("Seguy"       ,
                      "Moribondi"   ,
                      "Valder"      ,
                      "Moribondi"   ,
                      "Valder"     ,
                      "Roque")

Is_valid <- c("TRUE"   ,
              "FALSE"  ,
              "TRUE"   ,
              "TRUE"   ,
              "TRUE"   ,
              "FALSE")

df <- data.frame(Species_name, First_descriptor , Is_valid)

#  Colouring rows under a condition using gt table library

library(gt)
library(dplyr)

apply_style <- function(x, rows) {
  stopifnot(is.list(rows), length(rows) > 0)
  n <- length(rows)
  color <- c("red", "green", "lightblue", "black", "yellow", "pink") 
  col <- color[1:n]
  for (i in 1:n) {
    x <- x %>% tab_style(style = cell_fill(color = col[i]),
                         locations = cells_body(rows = rows[[i]]))
  }
  x
}


df %>%
  gt() %>%
  cols_align(align = c("left"),
             columns = everything()) %>%
  apply_style(rows = list(First_descriptor %in% "Seguy",
                          First_descriptor %in% "Valder",
                          !(First_descriptor %in% c("Seguy", "Valder")))
  )

image

I kept it short - if you need a more detailed explanation of what i am doing let me know.

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.