Emulate ggplot2 default color palette

后端 未结 4 1519
猫巷女王i
猫巷女王i 2020-11-22 09:13

What function can I use to emulate ggplot2\'s default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX color

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 09:46

    These answers are all very good, but I wanted to share another thing I discovered on stackoverflow that is really quite useful, here is the direct link

    Basically, @DidzisElferts shows how you can get all the colours, coordinates, etc that ggplot uses to build a plot you created. Very nice!

    p <- ggplot(mpg,aes(x=class,fill=class)) + geom_bar()
    ggplot_build(p)$data
    [[1]]
         fill  y count x ndensity ncount  density PANEL group ymin ymax xmin xmax
    1 #F8766D  5     5 1        1      1 1.111111     1     1    0    5 0.55 1.45
    2 #C49A00 47    47 2        1      1 1.111111     1     2    0   47 1.55 2.45
    3 #53B400 41    41 3        1      1 1.111111     1     3    0   41 2.55 3.45
    4 #00C094 11    11 4        1      1 1.111111     1     4    0   11 3.55 4.45
    5 #00B6EB 33    33 5        1      1 1.111111     1     5    0   33 4.55 5.45
    6 #A58AFF 35    35 6        1      1 1.111111     1     6    0   35 5.55 6.45
    7 #FB61D7 62    62 7        1      1 1.111111     1     7    0   62 6.55 7.45
    

提交回复
热议问题