# Exploring color palettes in R

How often have you had to squint at figures with unpleasant color palettes in a manuscript online or in print, and ultimately given up on distinguishing between fifty (or maybe just around 30) shades of gray? I found the RColorBrewer package extremely helpful when it comes to picking colors for figures – instead of the standard way of letting R decide your palette (using say ‘rainbow’, or ‘topo.colors’ – see this link). Here I describe some uses of RColorBrewer to make neat admixture bar-plots in R. You should be able to use the same color palettes for use in other kinds of plots as well (see my previous posts). Say you have a Q (admixture proportion) matrix obtained from your favorite program (STRUCTURE/ADMIXTURE/FASTRUCT/etc) – named q.txt. Here, I ran multinomial clustering with K = 3 subpopulations, requiring a three color palette from RColorBrewer. The data set that I used was mined from the Tishkoff lab as part of the supplementary material of a paper on microsatellite variation in African populations. Eg: “q.txt” –

```0  1   0
0.312204    0.687796    0
0   1   0
0   1   0
0   0.88985 0.11015
0.457319    0.542681    0
0.149153    0.850847    0
0.451845    0.477733    0.070422
0.405077    0.350571    0.244352
0   1   0
0   1   0
0.131876    0.707725    0.1604
...
```

To read the data file, install libraries:

```install.packages(“RColorBrewer”)
library(RColorBrewer)
barplot(t(as.matrix(q)),col=rainbow(3),xlab="Individual #", ylab="Ancestry",border=NA)
```

This should produce a bar plot with generic colors, picked using the ‘rainbow’ function.

To use ColorBrewer, I recommend playing around with different accent colors (you should be able to display them all using the display.brewer.pal(n, name) function. Alternately, you should be able to visualize a variety of schemes on the ColorBrewer2 website here.

For example:

```display.brewer.pal(3, “Greys”)
display.brewer.pal(6, “Accent”)
```

Thereon, create your own color palette using:

```mypal<-brewer.pal(3, “Accent”)
```

You could also let ColorBrewer decide red-green colorblind friendly palettes, using:

```mypal<-display.brewer.all(3, “Accent”, colorblindFriendly=TRUE)
```

Now you should be able to plot this directly by plugging your customized palette into the barplot function as:

```barplot(t(as.matrix(q)),col=mypal,xlab="Individual #", ylab="Ancestry",border=NA)
```

Here are two examples – one in grayscale, one using a spectral color scheme.

Speaking of colors, here are some spectacular images from Holi celebrations across the world! Happy Spring, everyone!

## About Arun Sethuraman

I am a computational biologist, and I build statistical models and tools for population genetics. I am particularly interested in studying the dynamics of structured populations, genetic admixture, and ancestral demography.
This entry was posted in howto, population genetics, R, software, STRUCTURE and tagged , , . Bookmark the permalink.