Colors (ggplot2)

Problem

You want to use colors in a graph with ggplot2.

Solution

The default colors in ggplot2 can be difficult to distinguish from one another because they have equal luminance. They are also not friendly for colorblind viewers.

A good general-purpose solution is to just use the colorblind-friendly palette below.

Sample data

These two data sets will be used to generate the graphs below.

  1. # Two variables
  2. df <- read.table(header=TRUE, text='
  3. cond yval
  4. A 2
  5. B 2.5
  6. C 1.6
  7. ')
  8. # Three variables
  9. df2 <- read.table(header=TRUE, text='
  10. cond1 cond2 yval
  11. A I 2
  12. A J 2.5
  13. A K 1.6
  14. B I 2.2
  15. B J 2.4
  16. B K 1.2
  17. C I 1.7
  18. C J 2.3
  19. C K 1.9
  20. ')

Simple color assignment

The colors of lines and points can be set directly using colour="red", replacing “red” with a color name. The colors of filled objects, like bars, can be set using fill="red".

If you want to use anything other than very basic colors, it may be easier to use hexadecimal codes for colors, like "#FF6699". (See the hexadecimal color chart below.)

  1. library(ggplot2)
  2. # Default: dark bars
  3. ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity")
  4. # Bars with red outlines
  5. ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity", colour="#FF9999")
  6. # Red fill, black outlines
  7. ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity", fill="#FF9999", colour="black")
  8. # Standard black lines and points
  9. ggplot(df, aes(x=cond, y=yval)) +
  10. geom_line(aes(group=1)) + # Group all points; otherwise no line will show
  11. geom_point(size=3)
  12. # Dark blue lines, red dots
  13. ggplot(df, aes(x=cond, y=yval)) +
  14. geom_line(aes(group=1), colour="#000099") + # Blue lines
  15. geom_point(size=3, colour="#CC0000") # Red dots

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

Mapping variable values to colors

Instead of changing colors globally, you can map variables to colors – in other words, make the color conditional on a variable, by putting it inside an aes() statement.

  1. # Bars: x and fill both depend on cond2
  2. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity")
  3. # Bars with other dataset; fill depends on cond2
  4. ggplot(df2, aes(x=cond1, y=yval)) +
  5. geom_bar(aes(fill=cond2), # fill depends on cond2
  6. stat="identity",
  7. colour="black", # Black outline for all
  8. position=position_dodge()) # Put bars side-by-side instead of stacked
  9. # Lines and points; colour depends on cond2
  10. ggplot(df2, aes(x=cond1, y=yval)) +
  11. geom_line(aes(colour=cond2, group=cond2)) + # colour, group both depend on cond2
  12. geom_point(aes(colour=cond2), # colour depends on cond2
  13. size=3) # larger points, different shape
  14. ## Equivalent to above; but move "colour=cond2" into the global aes() mapping
  15. # ggplot(df2, aes(x=cond1, y=yval, colour=cond2)) +
  16. # geom_line(aes(group=cond2)) +
  17. # geom_point(size=3)

plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4

A colorblind-friendly palette

These are color-blind-friendly palettes, one with gray, and one with black.

plot of chunk unnamed-chunk-5plot of chunk unnamed-chunk-5

To use with ggplot2, it is possible to store the palette in a variable, then use it later.

  1. # The palette with grey:
  2. cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
  3. # The palette with black:
  4. cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
  5. # To use for fills, add
  6. scale_fill_manual(values=cbPalette)
  7. # To use for line and point colors, add
  8. scale_colour_manual(values=cbPalette)

This palette is from http://jfly.iam.u-tokyo.ac.jp/color/:

Colorblind palette

Color selection

By default, the colors for discrete scales are evenly spaced around a HSL color circle. For example, if there are two colors, then they will be selected from opposite points on the circle; if there are three colors, they will be 120° apart on the color circle; and so on.The colors used for different numbers of levels are shown here:

plot of chunk unnamed-chunk-7

The default color selection uses scale_fill_hue() and scale_colour_hue(). For example, adding those commands is redundant in these cases:

  1. # These two are equivalent; by default scale_fill_hue() is used
  2. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity")
  3. # ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") + scale_fill_hue()
  4. # These two are equivalent; by default scale_colour_hue() is used
  5. ggplot(df, aes(x=cond, y=yval, colour=cond)) + geom_point(size=2)
  6. # ggplot(df, aes(x=cond, y=yval, colour=cond)) + geom_point(size=2) + scale_colour_hue()

plot of chunk unnamed-chunk-8plot of chunk unnamed-chunk-8

Setting luminance and saturation (chromaticity)

Although scale_fill_hue() and scale_colour_hue() were redundant above, they can be used when you want to make changes from the default, like changing the luminance or chromaticity.

  1. # Use luminance=45, instead of default 65
  2. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  3. scale_fill_hue(l=40)
  4. # Reduce saturation (chromaticity) from 100 to 50, and increase luminance
  5. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  6. scale_fill_hue(c=45, l=80)
  7. # Note: use scale_colour_hue() for lines and points

plot of chunk unnamed-chunk-9plot of chunk unnamed-chunk-9

This is a chart of colors with luminance=45:

plot of chunk unnamed-chunk-10

Palettes: Color Brewer

You can also use other color scales, such as ones taken from the RColorBrewer package. See the chart of RColorBrewer palettes below. See the scale section here for more information.

  1. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  2. scale_fill_brewer()
  3. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  4. scale_fill_brewer(palette="Set1")
  5. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  6. scale_fill_brewer(palette="Spectral")
  7. # Note: use scale_colour_brewer() for lines and points

plot of chunk unnamed-chunk-11plot of chunk unnamed-chunk-11plot of chunk unnamed-chunk-11

Palettes: manually-defined

Finally, you can define your own set of colors with scale_fill_manual(). See the hexadecimal code chart below for help choosing specific colors.

  1. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  2. scale_fill_manual(values=c("red", "blue", "green"))
  3. ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
  4. scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))
  5. # Note: use scale_colour_manual() for lines and points

plot of chunk unnamed-chunk-12plot of chunk unnamed-chunk-12

Continuous colors

[Not complete]

See the scale section here for more information.

  1. # Generate some data
  2. set.seed(133)
  3. df <- data.frame(xval=rnorm(50), yval=rnorm(50))
  4. # Make color depend on yval
  5. ggplot(df, aes(x=xval, y=yval, colour=yval)) + geom_point()
  6. # Use a different gradient
  7. ggplot(df, aes(x=xval, y=yval, colour=yval)) + geom_point() +
  8. scale_colour_gradientn(colours=rainbow(4))

plot of chunk unnamed-chunk-13plot of chunk unnamed-chunk-13

Color charts

Hexadecimal color code chart

Colors can specified as a hexadecimal RGB triplet, such as "#0066CC". The first two digits are the level of red, the next two green, and the last two blue. The value for each ranges from 00 to FF in hexadecimal (base-16) notation, which is equivalent to 0 and 255 in base-10. For example, in the table below, “#FFFFFF” is white and “#990000” is a deep red.

Colors (ggplot2) - 图25

(Color chart is from http://www.visibone.com)

RColorBrewer palette chart

plot of chunk unnamed-chunk-14