Slide 1


1 R Color Palettes cheatsheet R color This is for all of you who don’t know anything about color theory, and don’t care but want Finding a good color scheme for presenting data some nice colors on your map or figure...NOW! will help! cheatsheet can be challenging. This color R uses hexadecimal to represent colors When it comes to selecting a color palette, TIP: - Hexadecimal is a base 16 number system used to describe DO NOT try to handpick individual colors! You will color. Red, green, and blue are each represented by two waste a lot of time and the result will probably not ). Each character has 16 possible characters (# rrggbb be all that great. R has some good packages for symbols: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F: color palettes. Here are some of the options “00” can be interpreted as 0.0 and “FF” as 1.0 grDevices Packages: and grDevices = i.e., red #FF0000 , black=#000000, white = #FFFFFF palettes colorRamps cm.colors comes with the base grDevices Two additional characters (with the same scale) can be topo.colors added to the end to describe transparency (# rrggbbaa ) installation and colorRamps terrain.colors must be installed. Each palette’s R has 657 built in color names Example: heat.colors function has an argument for To see a list of names: rainbow peachpuff4 colors() see P. 4 for the number of colors and These colors are displayed on P. 3. options ): transparency ( alpha R translates various color models to hex, e.g.: heat.colors (4, alpha=1) RGB (red, green, blue): The default intensity scale in R • > #FF0000FF" "#FF8000FF" "#FFFF00FF" "#FFFF80FF“ ranges from 0 - 1; but another commonly used scale is 0 - =255. maxColorValue 255. This is obtained in R using For the palette you can also select start/end color rainbow alpha is an optional argument for transparency, with the 3/6 , cyan = 2/6 , green = 1/6 , blue (red = 0, yellow = same intensity scale. 4/6 5/6 and magenta = = ) and saturation (s) and value (v): (r, g, b, rgb =255, alpha=255) maxColorValue 1)/n, alpha = 1) rainbow(n, s = 1, v = 1, start = 0, end = max(1, n - • HSV (hue, saturation, value): values range from 0 - 1, with optional alpha argument Package: RcolorBrewer hsv (h, s, v, alpha) This function has an argument for the number of , luminance): hue describes the color and • HCL (hue, chroma colors and the color palette (see P. 4 for options). ranges from 0 - 360; 0 = red, 120 = green, blue = 240, etc. chroma and luminance depend on hue and each Range of brewer.pal(4, “Set3”) other > "#8DD3C7" "#FFFFB3" "#BEBADA" "#FB8072“ hcl (h, c, l, alpha) (5) display.brewer.all palettes in R: colorbrewer To view There is also a very nice interactive viewer: A few notes on HSV/HLC HSV is a better model for how humans perceive color. HCL can be thought of as a perceptually based version of blah blah ... the HSV model...blah ## My Recommendation ## Without delving into color theory: color schemes based colorspace Package: colorspace on HSV/HLC models generally just look good. These color palettes are based default palettes on HCL and HSV color models. diverge_hcl The results can be very diverge_hsl terrain_hcl aesthetically pleasing. There sequential_hcl are some default palettes: rainbow_hcl (4) rainbow_hcl "#E495A5" "#ABB065" "#39BEB1" "#ACA4E2“ However, all palettes are fully customizable: diverge_hcl(7, h = c(246, 40), c = 96, l = c(65, 90)) Choosing the values be daunting. But would there are some recommended palettes in the colorspace documentation. There is also an interactive tool that can be used to obtain a customized palette. To start the tool: R can translate colors to rgb (this is handy for choose_palette () pal < - matching colors in other programs) Page 1, Melanie Frazier col2rgb(c(“#FF0000”, “blue”))

2 How to use hex codes to define color R color cheatsheet using the plot function colorspace Overview of palette selector Discrete variables library(" ") colorspace Option 1 choose_palette () - pal < If you don’t need to control which colors are associated with each level of a variable: Select the type of color scheme plot( Sepal.Length ~ Sepal.Width , based on the type of data = rainbow_hcl (3)[c(Species)], col data=iris, =16) pch can be – Default color schemes used “as is” or as a starting point for modification ", topleft legend(" (3), rainbow_hcl = col =16, pch )) iris$Species legend=unique( Interactively select: • hue: color = gray chroma : low chroma • luminance: high luminance = • pastel power: how the color changes • along a gradient HCL hue Option 2 If you want to control which colors are associated with the levels of a variable, I find it easiest to create a variable in the data: Select # of colors in palette < - iris$Species iris$color , factor( virginica ", " "), levels=c(" setosa versicolor ", " Save palette for future R sessions: • txt file with hex codes (3)) labels= rainbow_hcl • .R file with a function describing , Sepal.Width ~ Sepal.Length plot( how to generate the palette. can be used to import the source col = as.character (color), pch =16, data=iris) function into R; but one complication is that you have to Continuous variables open the .R file and name the Option 1 function to use it. Break into categories and assign colors: Copy values into relevant • colorspace functions. subset(iris, Species==" ") iris2 < - setosa Diverging color schemes: - color < cut(iris2$Petal.Length, diverge_hcl (7, h = c(260, 0), c = 100, l = c(28, 90), power = 1.5) (3)) breaks=c(0,1.3,1.5,2), labels= sequential_hcl Sequential color schemes: Or, break by quantiles (be sure to include 0 & 1): (n, h, c.= c(), l=c(), sequential_hcl power) - cut(iris2$Petal.Length, color < Qualitative color schemes: , c(0, 0.25, 0.5, quantile breaks= iris$Petal.Length ( rainbow_hcl (n, c, l, start, end ) (for 0.75, 1)), labels= (3)) sequential_hcl schemes; start/ end qualtitative refer to the H1/H2 hue values) pch , Sepal.Length ~ Sepal.Width plot( =16, =color, data=iris2) col Display color scheme with Option 2 different plot types Fully continuous gradient: (10000), runif ("a"= data.frame - data < (10000)) "b"= runif ))[rank( data$a color= diverge_hcl (length( data$a )] =16, data=data) pch =color, col , a~b plot( For ggplot2, I think the most When “OK” is selected, the color palette flexible color scales are: will be saved in the R session. To return 7 scale_colour_manual hex color codes from the selected palette: () pal < - choose_palette scale_colour_gradient pal(7) for discrete and continuous [NOTE: These values are not saved if you Page 2, Melanie Frazier variables, respectively don’t save the session]

3 sheet - reference - color - and code to produce R color chart from: Page 4, Melanie Frazier

4 and grDevices colorRamps colorRamps and color palette, display from: grDevices - sheet/ reference - - color RColorBrewer To begin interactive color selector: choose_palette () - pal < Useful Resources: A larger color chart of R named colors: Sequential http://research.stowers - Nice overview of color in R: http://research.stowers - ments/colors Ver2.pdf Qualitative A color theory reference: Zeileis , A. K. Hornik , P. Murrell. 2009. Escaping : selecting colors for statistical graphics. RGBland Computational and Statistics & Data Analysis - 3270 53:3259 Diverging display.brewer.all RColorBrewer palette: To display () For interactive color selector: Page 4, Melanie Frazier

Related documents