rggplot2viridis

How to extract from a ggplot figure which Hex color codes were used in the viridis color palette?


After making a figure in ggplot and using a viridis palette, I would like to get a list of Hex color codes for all colors which were randomly selected for that particular plot. Is that possible?

Here is the data:

df = data.frame(Cell = c(rep("13a",5), rep("1b",5), rep("5b",5)),
                condition = rep(c("a","b","c","d","e"), 3),
                variable = c(58,55,36,29,53, 57,53,54,52,52, 45,49,48,46,45))

And the plot:

library(ggplot2)
library(viridis)

windows(width=4, height=4 )

ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
geom_point(aes(color = Cell))+
geom_line()+
scale_color_viridis(discrete=TRUE)

Solution

  • You can manually recreate the hexes used with a scales::viridis_pal()(n) call (proposed by @Gregor):

    scales::viridis_pal()(length(unique(df$Cell)))
    
    [1] "#440154FF" "#21908CFF" "#FDE725FF"
    

    However, you can also access the underlying data of any ggplot object using ggplot_build:

    Let's first save your plot as gg:

    gg <- ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
      geom_point(aes(color = Cell))+
      geom_line()+
      scale_color_viridis(discrete=TRUE)
    

    Now to access the underlying components:

    ggplot_build(gg)
    

    Since we really are only interested in the data:

    ggplot_build(gg)$data
    

    Which gives us:

    [[1]]
          colour x  y group PANEL shape size fill alpha stroke
    1  #440154FF 1 58     1     1    19  1.5   NA    NA    0.5
    2  #440154FF 2 55     1     1    19  1.5   NA    NA    0.5
    3  #440154FF 3 36     1     1    19  1.5   NA    NA    0.5
    4  #440154FF 4 29     1     1    19  1.5   NA    NA    0.5
    5  #440154FF 5 53     1     1    19  1.5   NA    NA    0.5
    6  #21908CFF 1 57     2     1    19  1.5   NA    NA    0.5
    7  #21908CFF 2 53     2     1    19  1.5   NA    NA    0.5
    8  #21908CFF 3 54     2     1    19  1.5   NA    NA    0.5
    9  #21908CFF 4 52     2     1    19  1.5   NA    NA    0.5
    10 #21908CFF 5 52     2     1    19  1.5   NA    NA    0.5
    11 #FDE725FF 1 45     3     1    19  1.5   NA    NA    0.5
    12 #FDE725FF 2 49     3     1    19  1.5   NA    NA    0.5
    13 #FDE725FF 3 48     3     1    19  1.5   NA    NA    0.5
    14 #FDE725FF 4 46     3     1    19  1.5   NA    NA    0.5
    15 #FDE725FF 5 45     3     1    19  1.5   NA    NA    0.5
    
    [[2]]
          colour x  y group PANEL size linetype alpha
    1  #440154FF 1 58     1     1  0.5        1    NA
    2  #440154FF 2 55     1     1  0.5        1    NA
    3  #440154FF 3 36     1     1  0.5        1    NA
    4  #440154FF 4 29     1     1  0.5        1    NA
    5  #440154FF 5 53     1     1  0.5        1    NA
    6  #21908CFF 1 57     2     1  0.5        1    NA
    7  #21908CFF 2 53     2     1  0.5        1    NA
    8  #21908CFF 3 54     2     1  0.5        1    NA
    9  #21908CFF 4 52     2     1  0.5        1    NA
    10 #21908CFF 5 52     2     1  0.5        1    NA
    11 #FDE725FF 1 45     3     1  0.5        1    NA
    12 #FDE725FF 2 49     3     1  0.5        1    NA
    13 #FDE725FF 3 48     3     1  0.5        1    NA
    14 #FDE725FF 4 46     3     1  0.5        1    NA
    15 #FDE725FF 5 45     3     1  0.5        1    NA