Colors for R graphs

Steve Simon


Categories: Blog post Tags: Graphical display R software

[StATS]: **Colors for R graphs (June 28

I tend to use color sparingly in graphs because most of my graphs end up in black and white in the final production. Even on my web pages

So when I did end up using color in a graph

plot(x,y,type="n") text(x[g==0],y[g==0],"C",col=2) text(x[g==1],y[g==1],"T",col=3)

in.print(width=8,height=10.5,printer="Adobe PDF") pa

would produce a graph where all the data points with the variable g equal to 0 would be red C’s and all the data points with the variable g equal to 1 would be green T’s. I never bothered figuring out how to get a particular color all that carefully because I never needed to worry too much about it.

But someone asked for a graph with black and gray lines

plot(x,y,type="n") lines(x[g==0],y[g==0],col="black") lines(x[g==1],y[g==1],col="gray")


I wrote a short program that produces all the colors in a PDF file.

win.print(width=8,height=10.5,printer="Adobe PDF") par(mar=rep(0,4)) ncolumns <- 7 nrows <- 100 npages <- trunc(length(colors())/(ncolumns*nrows))+1 for (i in 1:npages) { plot(c(0,(ncolumns+1)),c(0,nrows+1),xlab=" ",ylab=" ",axes=F,type="n") for (j in 1:nrows) { for (k in 1:ncolumns) { x <- ncolumns*nrows*(i-1)+(k-1)*nrows+j text(k,nrows+1-j,paste(x,"=",colors()[x]),col=colors()[x],cex=0.5) } } }

I named the PDF file


draws a series of points on the diagonal from the darkest green to the lightest green (see below).

ColorsForR.gif not found.

If you need a special range of colors for a contour plot or a heatmap, then you can use the palette() function. refer to the help function in R for details.

There is an excellent book which I have just started reading that provides much useful information about graphs in R.

This page was written by Steve Simon while working at Children’s Mercy Hospital. Although I do not hold the copyright for this material

software](../category/RSoftware.html). display](../category/GraphicalDisplay.html) or [Category: R for pages similar to this one at [Category: Graphical with general help resources. You can also browse Children’s Mercy Hospital website. Need more information? I have a page reproducing it here as a service