0% found this document useful (0 votes)
323 views

Using Color in R

This document discusses using color in R. It covers color basics like using palette colors, color names, and hex constants to specify colors. It also discusses using color for points, lines, axes, text, and backgrounds. Additionally, it explores color spaces like RGB and HSV, and how to create color gradients and ramps. Color blindness and differences between screen and printed colors are also mentioned. The document provides examples of specifying and using colors throughout graphics in R.

Uploaded by

Matteo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views

Using Color in R

This document discusses using color in R. It covers color basics like using palette colors, color names, and hex constants to specify colors. It also discusses using color for points, lines, axes, text, and backgrounds. Additionally, it explores color spaces like RGB and HSV, and how to create color gradients and ramps. Color blindness and differences between screen and printed colors are also mentioned. The document provides examples of specifying and using colors throughout graphics in R.

Uploaded by

Matteo
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Using Color in R

Stowers Institute for Medical Research


R/Bioconductor Discussion Group
Scientific Programmer 17 July 2007

Earl F. Glynn

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
2

Color Basics: Palette


> barplot(rep(1,8), yaxt="n", col=1:8)

Using Color in R

> palette()
[1] "black" "red" "green3" "gray"
3

"blue"

"cyan"

[6] "magenta" "yellow"

Color Basics: Palette


> barplot(rep(1,8), yaxt="n", col=1:8) > barplot(rep(1,8), yaxt="n", col=palette()) > barplot(rep(1,8), yaxt="n", col=c("black", "red", "green3", "blue", "cyan", "magenta", "yellow", "gray"))

Using Color in R

4 Integer color numbers represent offsets into the palette table

Color Basics: Palette


> palette(rainbow(10)) > palette() [1] "red" [6] "cyan" # Redefine palette

Using Color in R

RGB Hex Constants

Color Names
"#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#0066FF" "#3300FF" "#CC00FF" "#FF0099"

> # colors are "recycled" if necessary > barplot(rep(1,20),col=1:20, yaxt="n")

> palette("default") > palette() [1] "black" "blue" "red" "cyan"

Cut/Paste Bitmap
"green3" "gray"
Recycled Colors 5

[6] "magenta" "yellow"

Specify number of colors with rainbow

Color Basics: Palette


> 0:8 / 8 [1] 0.000 0.125 0.250 0.375 0.500 0.625 0.750 0.875 1.000 > palette(gray(0:8 / 8)) > palette() [1] "black" "#202020" "gray25" "#606060" "#808080"

Using Color in R

RGB Hex Constants

[6] "#9F9F9F" "gray75"

"#DFDFDF" "white"

> # colors are "recycled" if necessary > barplot(rep(1,20),col=1:20, yaxt="n") > > palette("default") > palette() [1] "black" "blue" "red" "cyan" "green3" Recycled Colors "gray" 6

[6] "magenta" "yellow"

Specify vector of floats 0.0 to 1.0 with grey/gray

Color Basics: Color Names


> colors() # or colours() [1] "white" [4] "antiquewhite1" [655] "yellow3" > colors()[grep("red", [1] "darkred" [4] "indianred2" [7] "mediumvioletred" [10] "orangered2" [13] "palevioletred" [16] "palevioletred3" [19] "red1" [22] "red4" [25] "violetred2" "aliceblue" "antiquewhite2" "yellow4" colors())] "indianred" "indianred3" "orangered" "orangered3" "palevioletred1" "palevioletred4" "red2" "violetred" "violetred3" "antiquewhite" "antiquewhite3" "yellowgreen"

Using Color in R

"indianred1" "indianred4" "orangered1" "orangered4" "palevioletred2" "red" "red3" "violetred1" "violetred4"

7 Color names from C:\Program Files\R\R-2.5.1\etc\rgb.txt

Color Basics: Color Names

Using Color in R

http://research.stowers-institute.org/efg/R/Color/Chart/index.htm

Color Basics: Color Names


Alphabetical except first color, which is white

Using Color in R

Print seven page table to compare screen colors with printed colors. 9 http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf

Color Basics: Hex Constants


Index Color Name #rrggbb red green blue
Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Using Color in R

"Unsaturated" shades of gray

"Saturated" Primary Colors

Hex "FF" = 15*161 + 15*160 = 255 Hex "00" to "FF" can be interpreted as 0.0 to 1.0.
10

Numbers represented in "base 16" are called "hexadecimal". Hex "FF" is largest value represented by one byte (8 bits).

Color Basics
In R Color can be represented by index into palette color name hex constant (24-bit "True Color": 2563 colors = 16,777,216 colors)

Using Color in R

Color Basics
In R many objects can take on different colors: points lines axes text legends background

Using Color in R

Color Basics: Points


> palette()
[1] "black" "red" [6] "magenta" "yellow" "green3" "gray" "blue" "cyan"
4

Using Color in R

> x <- -2:2 > y <- x^2

# Equivalent point colors > plot(x,y, col=1:5, pch=CIRCLE<-16, cex=2)

0 -2

Five (x,y) points to plot

-1

0 x

> plot(x,y, col=c("black", "red", "green3", "blue", "cyan"), pch=CIRCLE, cex=2) > plot(x,y, col=c("#000000", "#FF0000", "green3", 4, 5), pch=CIRCLE, cex=2)
Hex #rrggbb Color name Palette Index

Color Basics: Points


n <- 20 plot(1:n, pch=CIRCLE<-16, cex=1:n, col=rainbow(n))

Using Color in R

1:n

10

15

20

10 Index

15

20

Color Basics: Points


> plot(log2(somite$Signal.1L), log2(somite$Signal.1R))

Using Color in R

p=0
Colors 1 to 32

p=1

> library(gplots) > palette(rev(rich.colors(32)))

How to associate color with p-values at each point?

Color Basics: Points


> palette( rev(rich.colors(32)) ) # colors: 1 to 32 > plot(log2(somite$Signal.1L), log2(somite$Signal.1R), col=1 + 31*somite$worst.p) # col values from 1 to 32

Using Color in R

p=0

p=1

Color Basics: Points and Lines


300

Using Color in R

250

blue

100

150

library(scatterplot3d) ?scatterplot3d # Example 6

200

300 250 200 150 100 50 0 -50 -50 0 50 100 150 200 250 300

-50

red

Red-Green-Blue 3D Plot of colors()

green

50

Color Basics: Axes and Text


BOTTOM LEFT TOP RIGHT <<<<1 2 3 4
100 0 2

Using Color in R

Chart Title 4 6
Top

10 10 Right

par(col.lab="orange", col.main="gray") plot(0:10,0:10, col=0:10, pch=CIRCLE<-16, main="Chart Title", axes=FALSE, xlab="X axis",ylab="Y axis") # Margin Text mtext("Bottom", mtext("Left", mtext("Top", mtext("Right", BOTTOM, LEFT, TOP, RIGHT, col="red") col="green") col="blue") col="magenta") 10 in red
Y axis

80

60 Left

40

axis(BOTTOM, col="red", col.axis="red") # 2, ...

20

AXIS_LABEL_HORIZONTAL <- 1 axis(LEFT, col="green", col.axis="green", at=2*0:5, labels=paste(20*0:5), las=AXIS_LABEL_HORIZONTAL) axis(TOP, col="blue", col.axis="blue") axis(RIGHT, col="magenta", col.axis="magenta")

0 Bottom 0 2 4 X axis 6 8 10

Color Basics: Legends


Time <- 0:120
1.0 Expression -0.5 0.0 0.5

Using Color in R

Period1 <- cos(2*pi*Time/120) Period2 <- cos(2*pi*Time/90) Period3 <- cos(2*pi*Time/150)

COLORS <- c("red", "green", "blue") LINE.TYPES <- c("solid", "dotted", "dashed") Periods <- data.frame( Period1=Period1, Period2=Period2, Period3=Period3)

matplot(Periods, type = "l", xlab="Time[min]",ylab="Expression", col = COLORS, lty = LINE.TYPES) legend("bottomleft", c("120 min period", " 90 min period", "150 min period"), col = COLORS, lty = LINE.TYPES)

-1.0

120 min period 90 min period 150 min period 0 20 40 60 Time[min] 80 100 120

Color Basics: Background


par(bg="light blue") x <- -2:2 y <- x^2 plot(x,y, col=1:5, pch=CIRCLE<-16,cex=2)
y 4

Using Color in R

R's default background color is "transparent."

0 -2

-1

0 x

Set graphic background to simplify cutting and pasting to PowerPoint! Avoids in PowerPoint: Format Picture | Colors and Lines | Fill Color

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
21

Color Space: RGB Color Model


> rgb(1,0,0)

Using Color in R

[1] "#FF0000" > rgb(0,1,0) [1] "#00FF00" > rgb(1,1,0) [1] "#FFFF00" > rgb(255,0,0, maxColorValue=255) [1] "#FF0000" > col2rgb(c("blue", "yellow"))
red green blue [,1] [,2] 0 255 0 255 255 0

#rrggbb

R G B

Color is additive in the RGB Color Model. Coordinate values not always obvious.

22

Red-Green-Blue Color Cube


R = 0 to 255 G = 0 to 255 B = 0 to 255

Using Color in R

Blue
Cyan

255

Magenta

White
255, 255, 255

Not all colors are perceivable on all devices

Grey Line Black


0, 0, 0 255

Red
0.0 to 1.0 0 to 255 (8-bits)

Green 255

Yellow
24-bit graphics: 256 x 256 x 256 = 16,777,216 colors 256 shades of grey

Contrasting Colors?

Color Space: HSV


Match perception of color better than RGB

Using Color in R

Hue-Saturation-Value

Source: http://scien.stanford.edu/class/psych221/projects/02/sojeong/
24

> hsv(1,1,1) [1] "#FF0000" > hsv(1/3,1,1) [1] "#00FF00" > hsv(2/3,1,1) [1] "#0000FF"

Color Space: HSV


Hue-Saturation-Value
HSV (S=1, V=1)
0.275 0.300 0.325 0.350 0.250 0.225 0.200 0.175 0.150 0.125 0.100 0.075 0.050 0.025

Using Color in R

hue <- seq(0.0, 1.0, by=1/40) pie(rep(1,40), labels=formatC(hue, digits=3, format="f"), cex=0.75, col=hsv(hue, 1.0, 1.0), radius=1.0, main="HSV (S=1, V=1)" ) > rgb2hsv(col2rgb("blue")) [,1] h 0.6666667 s 1.0000000 Contrasting Colors? v 1.0000000
0.425 0.450

0.375 0.400

0.475 0.500

0.000 0.975

0.525 0.550 0.575 0.600 0.625 0.650 0.675 0.700 0.725 0.750 0.775 0.800 0.825

0.950 0.925 0.900 0.875 0.850

25

Color Spaces
help(package=colorspace)

Using Color in R

26

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
27

Color Gradients / Color Ramps


par(mfrow=c(6,1), mar=c(3,1,0,1)) BOTTOM <- 1
rainbow

Using Color in R

colorstrip <- function(colors, description, ShowAxis=FALSE) { count <- length(colors) m <- matrix(1:count, count, 1) image(m, col=colors, ylab="", axes=FALSE)

heat.colors

if (ShowAxis) { axis(BOTTOM) } mtext(description, BOTTOM, adj=0.5, line=0.5) }

terrain.colors

topo.colors

COLOR.COUNT <- 256 colorstrip(rainbow(COLOR.COUNT), "rainbow") colorstrip(heat.colors(COLOR.COUNT), "heat.colors") colorstrip(terrain.colors(COLOR.COUNT), "terrain.colors") colorstrip(topo.colors(COLOR.COUNT), "topo.colors") colorstrip(cm.colors(COLOR.COUNT), "cm.colors (cyan-magenta)") colorstrip(gray(0:COLOR.COUNT / COLOR.COUNT),"gray")

cm.colors (cyan-magenta)

gray

28

Color Gradients / Color Ramps


colorRamp and colorRampPalette added in R 2.1.0
1.0 1.0

Using Color in R

m <- outer(1:20,1:20,function(x,y) sin(sqrt(x*y)/3)) rgb.palette <- colorRampPalette(c("red", "orange", "blue"), space = "rgb") filled.contour(m,col = rgb.palette(20))

0.8 0.5

0.6 0.0 0.4

-0.5 0.2

0.0 0.0 0.2 0.4 0.6 0.8 1.0

-1.0

1.0

1.0

# space="Lab" helps when colors don't form a # natural sequence Lab.palette <- colorRampPalette(c("red", "orange", "blue"), space = "Lab") filled.contour(m,col = Lab.palette(20))

0.8 0.5

0.6 0.0 0.4

-0.5 0.2

0.0 0.0 0.2 0.4 0.6 0.8 1.0

29
-1.0

Color Gradients / Color Ramps


HCL (Hue, Chroma, Luminance)
> ?hcl > barplot(rep(1, 20), col = hcl(seq(0, 360, length = 20)))

Using Color in R

See: HCL(Hue-Chroma-Luminance)-based Color Palettes in R http://cran.r-project.org/doc/vignettes/vcd/hcl-colors.pdf

0.0

0.4

0.8

30

Color Gradients / Color Ramps


RColorBrewer Package
> library(RColorBrewer) > display.brewer.all()
YlOrRd YlOrBr YlGnBu YlGn Reds RdPu Purples PuRd PuBuGn PuBu OrRd Oranges Greys Greens GnBu BuPu BuGn Blues Set3 Set2 Set1 Pastel2 Pastel1 Paired Dark2 Accent Spectral RdYlGn RdYlBu RdGy RdBu PuOr PRGn PiYG BrBG

Using Color in R

31

Color Gradients / Color Ramps


Representative Color for Given Wavelength

Using Color in R

Source: http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm

32

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
33

Color Blindness
Ishihara Test for Color Blindness

Using Color in R

Normal Color Vision: 25, 29, 45, 56, 6, 8 Red-Green Color Blind: 25, spots, spots, 56, spots, spots
Source: http://www.toledo-bend.com/colorblind/Ishihara.html

About 1 in 12 have some sort of color deficiency: About ~8% of men and ~0.4% of women in the US.
http://en.wikipedia.org/wiki/Color_blindness 34

Color Blindness
R dichromat Package: Color Schemes for dichromats
library(dichromat) par(mfcol=c(1,2)) N <- 20 pie(rep(1,N),col=heat.colors(N)) pie(rep(1,N),col=dichromat(heat.colors(N)))

Using Color in R

dichromat function collapses red-green color distinctions to approximate the effect of the two 35 common forms of red-green color blindness, protanopia and deuteranopia.

Color Blindness
R dichromat Package: Color Schemes for dichromats
library(dichromat) par(mar=c(1,2,1,1)) layout(matrix(1:6,ncol=1)) image(1:10,1,matrix(1:10, ncol=1), col=colorschemes$BrowntoBlue.10, main="Brown to Blue (10)", axes=FALSE) image(1:100,1,matrix(1:100 ,ncol=1), col=colorRampPalette(colorschemes$BrowntoBlue.10,space="Lab")(100), main="Brown to Blue Ramp", axes=FALSE) image(1:10,1,matrix(1:10, ncol=1), col=dichromat(colorschemes$BrowntoBlue.10), main="Brown to Blue (10) -- deuteranopia", axes=FALSE) image(1:12,1,matrix(1:12, ncol=1),col=colorschemes$Categorical.12, main="Categorical (12)", axes=FALSE) image(1:12,1,matrix(1:12, ncol=1), col=dichromat(colorschemes$Categorical.12), main="Categorical (12) -- deuteranopia", axes=FALSE) image(1:12,1,matrix(1:12,ncol=1), col=dichromat(colorschemes$Categorical.12, "protan"), main="Categorical (12) -- protanopia", axes=FALSE)

Using Color in R

36

Color Blindness
R dichromat Package: Color Schemes for dichromats
Brown to Blue (10)

Using Color in R

Brown to Blue Ramp

Brown to Blue (10) -- deuteranopia

Categorical (12)

Categorical (12) -- deuteranopia

Categorical (12) -- protanopia

37

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
38

Why Dont Screen Colors Match Printout?

Using Color in R

Conceptual Diagram of All Colors (2D Slice)

39

Why Dont Screen Colors Match Printout?

Using Color in R

Each Device Has Own Gamut of Colors

40

3D Color Gamut

Using Color in R

From Visualization of Expanded Printing Gamuts Using 3-Dimensional Convex Hulls by Karl Guyler, Hallmark Cards, Kansas City

Color calibration can be used to minimize needless differences

Why Dont Screen Colors Match Printout?


Different

Using Color in R

color gamuts between devices:

How should a color be represented if it doesn't exist on a device?

Imperfect conversions:
CRT screens with RGB (Red-Green-Blue) additive colors must be converted to CMYK (Cyan-Magenta-Yellow-Black) subtractive colors

Color fidelity may not be accurate Paper/ink differences


42

Using Color in R
Color

Basics in R

Color Spaces Color Gradients / Color Ramps Color Blindness


Why Dont Screen Colors Match Printout? Colors Tips
43

Color Tips
Avoid

Using Color in R

unnecessary use of color.

Use bright colors with small graphics to make them stand out. Be consistent in use of color. Don't use color as only attribute to show difference. E.g., consider color and line type.
Also see: Cool Color Commentary, http://www.public-speaking.org/public-speaking-color-article.htm 44

Color Tips: Palettes


Qualitative Palette:
all same perceptual weight/importance typical application: bar plot

Using Color in R

Sequential Palette
for coding numerical information in a range typical application: heat map

Diverging Palette
Like Sequential Palette but with neutral value
Source: Choosing Color Palettes for Statistical Graphics 45 http://epub.wu-wien.ac.at/dyn/virlib/wp/mediate/epub-wu-01_abd.pdf?ID=epub-wu-01_abd

References
Colour for Presentation Graphics
http://www.stat.auckland.ac.nz/~ihaka/colour/color-talk.pdf

Using Color in R

Why should Engineers and Scientists be worried about color?


http://www.research.ibm.com/people/l/lloydt/color/color.HTM

46

You might also like