Search code examples
rcolors

Visualise HEX colours from a data frame list in R


I have a dataframe like this:

test <- data.frame(TYPE=c("BIG", "BIG","BIG","SMALL","SMALL","SMALL"))
test$cols <- list(c("#2A2828", "#84828D"), c("#A27364","#F1F0F0","#EFC6A4"),c("#DE9E86","#F94C40"),c("#DDDADE","#CDCFD3"),c("#A3A2A3","#D2AA9E"),c("#B1BDC9", "#A3A2A3") )

Id like to visualise/plot the HEX colour codes that are held as list elements inside the datafranme cells, split by TYPE. I'm not sure of the best way to do this as my data frame can get much bigger than this example. I would ideally like 2 plots (one for each TYPE) then somehow all of the colours for each type displayed in a way that allows one to 'see' what colours each TYPE are made up of.

Any ideas would be great.


Solution

  • Something like this?

    test %>% 
      unnest(cols) %>%
      ggplot() +
      geom_tile(aes(x = cols, y = TYPE, fill = cols), height = .5) +
      scale_fill_identity() +
      theme_void() +
      facet_wrap(~ TYPE, scales = "free") +
      theme(strip.text = element_text(size=25))
    

    enter image description here