automatically build the perfect palette for you plot …...automatically build the perfect palette...
TRANSCRIPT
automatically build the perfect palette for you plot with paletteRAndrea Cirillo9th MilanoR meeting, 20th November 2017
paletteR: palettes from custom images
• takes any image
• applies statistical learning
• builds a palette
draw from any image an optimised custom palette
takes any imagex y R G B
1 275 0,9960784313725 0,9960784313725 0,9960784313725
1 274 0,9960784313725 0,9960784313725 0,9960784313725
1 273 0,9960784313725 0,9960784313725 0,9960784313725
1 272 0,9960784313725 0,9960784313725 0,9960784313725
1 271 0,9960784313725 0,9960784313725 0,9960784313725
1 270 0,9803921568627 1,0000000000000 0,9803921568627
1 269 0,9803921568627 1,0000000000000 0,9803921568627
1 268 0,9843137254902 1,0000000000000 0,9882352941176
1 267 0,9882352941176 1,0000000000000 0,9921568627451
1 266 0,9960784313725 1,0000000000000 1,0000000000000
1 263 0,9960784313725 0,9960784313725 1,0000000000000
1 257 0,9960784313725 1,0000000000000 1,0000000000000
1 256 0,9960784313725 1,0000000000000 1,0000000000000
1 255 0,9960784313725 1,0000000000000 1,0000000000000
1 254 0,9921568627451 0,9960784313725 1,0000000000000
readJPEG(…)
applies statistical learning
kmeans(painting_rgb[,c(“R","G","B")],centers = number_of_colors, iter.max = 30)
applies statistical learning
R B
G
builds a palette
rgb_raw_palette <- k_means$centers final_palette <- rgb(k_means$centers)show_col(final_palette)
builds a palette
no knowledge about relationships between colours
no order in colours
too dark and too bright colours
interlude: rgb and hsv
By SharkD - Own work, GFDL, https://commons.wikimedia.org/w/index.php?curid=3375025
RGB
interlude: rgb and hsv
By SharkD - Own work, GFDL, https://commons.wikimedia.org/w/index.php?curid=3375025
RGB
Hue
Value
HSV
paletteR gots cleaver
• learning colours
• palette optimization based on variable type
learning coloursrgb_raw_palette %>% t() %>% rgb2hsv() %>% t() %>% data.frame() %>% mutate(id = row.names(.)) -> hsv_raw_palette
learning coloursrgb_raw_palette %>% data.frame() %>% mutate(id = row.names(.)) %>% left_join(hsv_raw_palette) %>% left_join(rgb_text_vector) %>% arrange(h,s,v) -> sorted_raw_palette
learning coloursbrightness_stats <- boxplot.stats(sorted_raw_palette$v)saturation_stats <- boxplot.stats(sorted_raw_palette$s)first_quartile_v <- round(brightness_stats$stats[2],4) sorted_raw_palette %>% filter(v > first_quartile_v) -> sorted_raw_palette outlier_threshold_v <- brightness_stats$stats[5] sorted_raw_palette %>% filter(v < outlier_threshold_v) -> sorted_raw_palette sorted_raw_palette %>% filter(s > first_quartile_s) -> sorted_raw_palette
palette optimization based on variable type
hue
dens
ity
palette optimization based on variable type
categorical
• algorithmic creation of alternative samples of hues
• evaluation of relative divergence among contiguous colours
• selection of the best sequence hue
dens
ity
palette optimization based on variable type
continuous
• hue distribution mode selection
• colours interpolation between mode and contiguous colour
hue
dens
ity
the final palette
how to use itdevtools::install_github("andreacirilloac/paletter")
library(paletter)
colours_vector <- create_palette(image_path = image_path,number_of_colors =32,type_of_variable = “categorical")
how to use itggplot(data = mtcars, aes(x = rownames(mtcars),y = hp,color = rownames(mtcars), fill = rownames(mtcars))) + geom_bar(stat = 'identity') + scale_color_manual(values = colours_vector) + scale_fill_manual(values=colours_vector)+ theme_minimal() + guides(size = FALSE) + theme(legend.position = "bottom") + labs(title = "disp vs hp")+ coord_flip()