How to manage colors with R.
Transcription
How to manage colors with R.
How to manage colors with R. Yan Holtz | [email protected] 02/07/2015 Contents How computer colors work ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Calling colors by their name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Using colors palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Using the RGB() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Using the hsv() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 How computer colors work ? For a computer, a color is a mix between 3 main colors : Red, Green and Blue. (RGB color space). Each pixel is constituted with 3 points that are to small for the Human eye, one of each colors, each color beeing more or less strong. But it is not easy to build your desire color. How can we do with the R software ? Calling colors by their name. 657 colors are available in R. To see their names, just type : colors() Then it is easy to call one of these colors for a plot plot(c(1,2) , c(1,1) , axes=F , col=c("blue" , "Darkgreen") , pch=20 , cex=14 , xlim=c(0,3) , xlab="" , Using colors palette If you need severals colors in the same palette, you can use one of the folowing functions : rainbow() ; heat.colors() ; terrain.colors() ; topo.colors() ; cm.colors() To use them, you have to indicate the number of different colors you want, and the transparency needed. Exemple : 1 heat.colors(5 , alpha=0.4) ## [1] "#FF000066" "#FF550066" "#FFAA0066" "#FFFF0066" "#FFFF8066" You have 5 different colors going from cold to hot!! So you can plot 20 points with 20 different colors to get a nice draw n <- 20; y <- -sin(3*pi*((1:n)-1/2)/n) plot(y, axes = FALSE, frame.plot = TRUE, xlab = "", ylab = "", pch = 21, cex = 30, bg = rainbow(n, alpha Using the RGB() function The rgb() function describes the color giving the intensity of the 3 primary colors. To use the function : rgb(red, green, blue, alpha) : quantity of red (between 0 and 1), of green and of blue, and finally transparency. exemple : rgb(1,0.5,0.1,0.5) ## [1] "#FF801A80" That’s a kind of Orange Using the hsv() function This function permits to define a color using 4 parameters : Hue, Saturation, Value and Transparency, that are between 0 and 1. Example : hsv(h=1 , s=1 , v=1 , 0.9) ## [1] "#FF0000E6" 2 Explanation of each variable : h = hue. It defines the basis color. The plot below gives you an idea of the 0 20 40 60 80 value of Hue value needed for each color (h going from 0 to 1): s= saturation, entre 0 et 1, La Saturation caractérise la vivacité, la densité ou encore la pureté d’une couleur. Ainsi, lorsque l’on parle d’une « couleur vive », c’est qu’il s’agit d’une couleur dont la saturation est maximale (100%). A l’opposé, on aura une couleur « terne », voire désaturée, qui finira par correspondre à un gris 0 neutre (0%). 20 40 60 80 value of Hue v=value la valeur minimale (0%) correspond toujours à une absence de luminosité, donc au noir Les valeurs proches de 1 amènent plus de brillance ou de luminosité alpha = valeur de la transparence 3