using color in r

Upload: matteo

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Using Color in R

    1/46

    1

    St ower s I nst it ut e f or Medical Resear chR/ Bioconduct or Discussion Gr oup

    Ear l F. GlynnScient if ic Pr ogr ammer

    17 July 2007

    Using Color in R

  • 7/31/2019 Using Color in R

    2/46

    2

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    3/46

    3

    Using Color in R

    Color Basics: Palet t e> barplot(rep(1,8), yaxt="n", col=1:8)

    > palette()

    [1] "black" "red" "green3" "blue" "cyan"

    [6] "magenta" "yellow" "gray"

  • 7/31/2019 Using Color in R

    4/46

    4

    Using Color in R

    Color Basics: Palet t e> 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"))

    Integer color numbers represent offsets into the palette table

  • 7/31/2019 Using Color in R

    5/46

    5

    Using Color in R

    Color Basics: Palet t e> palette(rainbow(10)) # Redefine palette

    > palette()

    [1] "red" "#FF9900" "#CCFF00" "#33FF00" "#00FF66"

    [6] "cyan" "#0066FF" "#3300FF" "#CC00FF" "#FF0099"

    > # colors are "recycled" if necessary

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

    > palette("default")

    > palette()

    [1] "black" "red" "green3"

    "blue" "cyan"

    [6] "magenta" "yellow" "gray"

    RGB Hex Constants

    Color Names

    Specify number of colors withrainbow

    Recycled Colors

    Cut/Paste

    Bitmap

  • 7/31/2019 Using Color in R

    6/46

    > 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"

    [6] "#9F9F9F" "gray75" "#DFDFDF" "white"

    > # colors are "recycled" if necessary

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

    >

    > palette("default")

    > palette()

    [1] "black" "red" "green3"

    "blue" "cyan"

    [6] "magenta" "yellow" "gray"

    6

    Using Color in R

    Color Basics: Palet t e

    RGB Hex Constants

    Recycled Colors

    Specify vector of floats 0.0 to 1.0 withgrey/gray

  • 7/31/2019 Using Color in R

    7/46

    > colors() # or colours()

    [1] "white" "aliceblue" "antiquewhite"

    [4] "antiquewhite1" "antiquewhite2" "antiquewhite3"

    [655] "yellow3" "yellow4" "yellowgreen"

    > colors()[grep("red", colors())][1] "darkred" "indianred" "indianred1"

    [4] "indianred2" "indianred3" "indianred4"

    [7] "mediumvioletred" "orangered" "orangered1"

    [10] "orangered2" "orangered3" "orangered4"

    [13] "palevioletred" "palevioletred1" "palevioletred2"

    [16] "palevioletred3" "palevioletred4" "red"

    [19] "red1" "red2" "red3"

    [22] "red4" "violetred" "violetred1"[25] "violetred2" "violetred3" "violetred4"

    7

    Using Color in R

    Color Basics: Color Names

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

  • 7/31/2019 Using Color in R

    8/46

    8

    Using Color in R

    Color Basics: Color Names

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

  • 7/31/2019 Using Color in R

    9/46

    9

    Using Color in R

    Color Basics: Color Names

    Print seven page table to compare screen colors with printed colors.

    http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf

    Alphabet icalexcept f ir stcolor , which iswhit e

  • 7/31/2019 Using Color in R

    10/46

    10

    Using Color in R

    Color Basics: Hex Const ant s

    Numbers represented in "base 16" are called "hexadecimal".

    Hex "FF" is largest value represented by one byte (8 bits).

    Hex Decimal

    0 0

    1 1

    2 2

    3 34 4

    5 5

    6 6

    7 7

    8 8

    9 9

    A 10

    B 11C 12

    D 13

    E 14

    F 15

    Index Color Name #rrggbb red green blue

    Hex "FF" = 15*161 + 15*160 = 255

    Hex "00" to "FF" can be interpreted as 0.0 to 1.0.

    "Sat ur at ed"Pr imar y Color s

    "Unsat ur at ed"shades of gray

  • 7/31/2019 Using Color in R

    11/46

    Using Color in R

    Color BasicsI n R Color can be repr esent ed by

    index int o palet t e

    color name

    hex const ant (24-bit "Tr ue Color ":

    2563

    color s = 16,777,216 color s)

  • 7/31/2019 Using Color in R

    12/46

    Using Color in R

    Color BasicsI n R many obj ect s can t ake on dif f er ent color s:

    point s

    l ines

    axes

    t ext

    legends

    backgr ound

  • 7/31/2019 Using Color in R

    13/46

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

    [6] "magenta" "yellow" "gray"

    > x y plot(x,y, col=1:5,

    pch=CIRCLE 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

    Using Color in R

    Color Basics: Point s

    -2 -1 0 1 2

    0

    1

    2

    3

    4

    x

    y

  • 7/31/2019 Using Color in R

    14/46

    Using Color in R

    Color Basics: Point sn

  • 7/31/2019 Using Color in R

    15/46

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

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

    p=1p=0

    > library(gplots)

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

    Colors 1 to 32

    Using Color in R

    Color Basics: Point s

  • 7/31/2019 Using Color in R

    16/46

    > 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

    p=1p=0

    Using Color in R

    Color Basics: Point s

  • 7/31/2019 Using Color in R

    17/46

    Using Color in R

    Color Basics: Point s and Lines

    -50 0 50 100 150 200 250 300

    -50

    0

    50

    100

    150

    200

    250

    300

    -500

    50100

    150200 250

    300

    red

    green

    blue

    Red-Green-Blue 3D Plot ofcolors()

    library(scatterplot3d)

    ?scatterplot3d

    # Example 6

  • 7/31/2019 Using Color in R

    18/46

    Using Color in R

    Color Basics: Axes and TextBOTTOM

  • 7/31/2019 Using Color in R

    19/46

    Using Color in R

    Color Basics: LegendsTime

  • 7/31/2019 Using Color in R

    20/46

    Using Color in R

    Color Basics: Backgr ound

    par(bg="light blue")

    x

  • 7/31/2019 Using Color in R

    21/46

    21

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    22/46

    22

    Using Color in R

    Color Space: RGB Color Model

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

    #rrggbb R G B

    > rgb(1,0,0)

    [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"))

    [,1] [,2]

    red 0 255green 0 255

    blue 255 0

  • 7/31/2019 Using Color in R

    23/46

    Using Color in R

    Red-Gr een-Blue Color Cube

    Red

    Green

    Blue

    Yellow

    Magenta

    Cyan

    Black

    White

    Grey Line

    255

    0, 0, 0

    255, 255, 255

    255

    255

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

    0.0 to 1.0

    0 to 255 (8-bits)

    24-bit graphics:

    256 x 256 x 256 = 16,777,216 colors

    256 shades of grey

    Not all colors

    are perceivable

    on all devices

    Cont rast ing Colors?

  • 7/31/2019 Using Color in R

    24/46

    24

    Using Color in R

    Color Space: HSV

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

    Hue-Sat ur at ion-ValueMat ch percept ion of color bet t er t han RGB

  • 7/31/2019 Using Color in R

    25/46

    25

    Using Color in R

    Color Space: HSVHue-Sat ur at ion-Value

    > hsv(1,1,1)

    [1] "#FF0000"

    > hsv(1/3,1,1)

    [1] "#00FF00"

    > hsv(2/3,1,1)

    [1] "#0000FF"

    hue rgb2hsv(col2rgb("blue"))

    [,1]

    h 0.6666667

    s 1.0000000

    v 1.0000000

    0.000

    0.025

    0.050

    0.075

    0.100

    0.125

    0.150

    0.1750.200

    0.2250.2500.275

    0.300

    0.325

    0.350

    0.375

    0.400

    0.425

    0.450

    0.475

    0.500

    0.525

    0.550

    0.575

    0.600

    0.625

    0.650

    0.6750.700

    0.725 0 .7 500.775

    0.800

    0.825

    0.850

    0.875

    0.900

    0.925

    0.950

    0.975

    HSV (S=1, V=1)

    Cont r ast ing Colors?

  • 7/31/2019 Using Color in R

    26/46

    26

    Using Color in R

    Color Spaceshelp(package=color space)

  • 7/31/2019 Using Color in R

    27/46

    27

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    28/46

    28

    Using Color in R

    Color Gr adient s / Color Ramps

    rainbow

    heat.colors

    terrain.colors

    topo.colors

    cm.colors (cyan-magenta)

    gray

    par(mfrow=c(6,1), mar=c(3,1,0,1))

    BOTTOM

  • 7/31/2019 Using Color in R

    29/46

    29

    Using Color in R

    Color Gr adient s / Color Rampscolor Ramp and colorRampPalet t e added in R 2.1.0

    m

  • 7/31/2019 Using Color in R

    30/46

    30

    Using Color in R

    Color Gr adient s / Color RampsHCL (Hue, Chr oma, Luminance)

    > ?hcl

    > barplot(rep(1, 20), col = hcl(seq(0, 360, length = 20)))

    0.0

    0.4

    0.8

    See: HCL(Hue-Chr oma-Luminance)-based Color Palet t es in R

    ht t p:/ / cran.r -proj ect .or g/ doc/ vignet t es/ vcd/ hcl-color s.pdf

  • 7/31/2019 Using Color in R

    31/46

    31

    Using Color in R

    Color Gr adient s / Color Ramps

    > library(RColorBrewer)

    > display.brewer.all()

    RColorBr ewer Package

    BrBGPiYG

    PRGnPuOrRdBuRdGy

    RdYlBuRdYlGn

    Spectral

    AccentDark2PairedPastel1

    Pastel2Set1Set2Set3

    BluesBuGnBuPuGnBu

    GreensGreys

    OrangesOrRdPuBu

    PuBuGnPuRd

    PurplesRdPuRedsYlGnYlGnBu

    YlOrBrYlOrRd

  • 7/31/2019 Using Color in R

    32/46

    32

    Using Color in R

    Color Gr adient s / Color RampsRepr esent at ive Color f or Given Wavelengt h

    Sour ce: ht t p:/ / www.ef g2.com/ Lab/ ScienceAndEngineer ing/ Spect r a.ht m

  • 7/31/2019 Using Color in R

    33/46

    33

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    34/46

    34

    Using Color in R

    Color Blindness

    Nor mal Color Vision: 25, 29, 45, 56, 6, 8Red-Gr een Color Blind: 25, spot s, spot s, 56, spot s, spot s

    I shihar a Test f or Color Blindness

    Sour ce: ht t p:/ / www.toledo-bend.com/ colorblind/ I shihar a.ht ml

    About 1 in 12 have some sort of color deficiency:About ~8% of men and ~0.4% of women in the US.

    ht t p:/ / en.wikipedia.or g/ wiki/ Color_blindness

  • 7/31/2019 Using Color in R

    35/46

    35

    Using Color in R

    Color BlindnessR dichromat Package: Color Schemes f or dichromat s

    library(dichromat)

    par(mfcol=c(1,2))

    N

  • 7/31/2019 Using Color in R

    36/46

    36

    Using Color in R

    Color BlindnessR dichromat Package: Color Schemes f or dichromat s

    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)

  • 7/31/2019 Using Color in R

    37/46

    37

    Using Color in R

    Color BlindnessR dichromat Package: Color Schemes f or dichromat s

    Brown to Blue (10)

    Brown to Blue Ramp

    Brown to Blue (10) -- deuteranopia

    Categorical (12)

    Categorical (12) -- deuteranopia

    Categorical (12) -- protanopia

  • 7/31/2019 Using Color in R

    38/46

    38

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    39/46

    39

    Using Color in R

    Why Dont Screen Colors Mat ch Pr int out ?

    Concept ualDiagram ofAl l Colors(2D Slice)

  • 7/31/2019 Using Color in R

    40/46

    40

    Using Color in R

    Why Dont Screen Colors Mat ch Pr int out ?

    Each DeviceHas OwnGamut ofColors

  • 7/31/2019 Using Color in R

    41/46

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

    Color calibr at ion can be used t o minimize needless dif f erences

    Using Color in R

    3D Color Gamut

  • 7/31/2019 Using Color in R

    42/46

    42

    Using Color in R

    Why Dont Screen Colors Mat ch Pr int out ?

    Dif f er ent color gamut s bet ween devices:How should a color be repr esent ed if it doesn' t exist

    on a device?

    I mper f ect conver sions:CRT screens wit h RGB (Red-Gr een-Blue) addit ive

    colors must be conver t ed t o CMYK

    (Cyan-Magent a-Yellow-Black) subt r act ive colors

    Color f idelit y may not be accur at e

    Paper / ink dif f er ences

  • 7/31/2019 Using Color in R

    43/46

    43

    Using Color in R Color Basics in R

    Color Spaces

    Color Gr adient s / Color Ramps

    Color Blindness

    Why Dont Screen Colors Mat ch Pr int out ?

    Color s Tips

  • 7/31/2019 Using Color in R

    44/46

    44

    Using Color in R

    Color Tips Avoid unnecessar y use of color .

    Use br ight color s wit h smallgr aphics t o make t hem st and out .

    Be consist ent in use of color .

    Don' t use color as only at t r ibut et o show dif f er ence. E.g., considercolor and line t ype.

    Also see: Cool Color Comment ar y, ht t p:/ / www.public-speaking.or g/ public-speaking-color- ar t icle.htm

  • 7/31/2019 Using Color in R

    45/46

    45

    Using Color in R

    Color Tips: Palet t esQualit at ive Palet t e:

    all same per cept ual weight / impor t ance

    t ypical applicat ion: bar plot

    Sequent ial Palet t e

    f or coding numer ical inf or mat ion in a r ange

    t ypical applicat ion: heat map

    Diver ging Palet t e

    Like Sequent ial Palet t e but wit h neut r al value

    Sour ce: Choosing Color Palet t es f or St at ist ical Gr aphicsht t p:/ / epub.wu-wien.ac.at / dyn/ vir lib/ wp/ mediat e/ epub-wu-01_abd.pdf ?I D=epub-wu-01_abd

  • 7/31/2019 Using Color in R

    46/46

    46

    Using Color in R

    Ref er encesColour for Presentation Graphicshttp://www.stat.auckland.ac.nz/~ihaka/colour/color-talk.pdf

    Why should Engineers and Scientists be worried about color?http://www.research.ibm.com/people/l/lloydt/color/color.HTM