ggplot2 default color palette

Published by onesixx on

https://stackoverflow.com/questions/25211078/what-are-the-default-plotting-colors-in-r-or-ggplot2/25211125#25211125
https://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette
https://stackoverflow.com/questions/33221794/separate-palettes-for-facets-in-ggplot-facet-grid

ggplot 정보 확인

p <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_point()
p    
summary(p)
ggplot_build(p)$data
ggplot_build(p)$layout
ggplot_build(p)$plot

ggplot 정보 확인 (data)

ggplot_build(p)$data
     colour   x   y PANEL group shape size fill alpha stroke
1   #F8766D 5.1 1.4     1     1    19  1.5   NA    NA    0.5
...
51  #00BA38 7.0 4.7     1     2    19  1.5   NA    NA    0.5
...
101 #619CFF 6.3 6.0     1     3    19  1.5   NA    NA    0.5
...

emulate  ggplot2’s   default color palette

“The default colour scheme, scale_colour_hue picks evenly spaced hues around the hcl colour wheel”
(From 106 page of the ggplot2 book by Hadley Wickham)

library(scales)

show_col(hue_pal()(3))
show_col(hue_pal()(4))
[1] "#F8766D" "#00BA38" "#619CFF"
[1] "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"

 

 

Color 설정하여 그리기

library(scales)
p <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species))
iris2 <- cbind(iris, ggcolor=ggplot_build(p)$data[[1]][, 1])

p <- ggplot(iris2, aes(x=Sepal.Length, y=Petal.Length, color=ggcolor)) + 
        geom_point() 
p

 

p <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species))
groupOfColor <- ggplot_build(p)$data[[1]][, 1] %>% unique
names(groupOfColor) <- iris2$Species %>% unique
# groupOfColor

p <- ggplot(iris2, aes(x=Sepal.Length, y=Petal.Length, color=Species)) + 
    geom_point() +
    scale_color_manual(values=groupOfColor)
p

 

Categories: ggplot2

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.