Tuesday, 15 January 2019

Using RColorBrewer to choose colours for R plots

I wanted to make a plot in R where the colour of a line depends on the amount of data (amount of sequence reads) supporting it. To do this, I decided to try the RColorBrewer library. This is an R version of ColorBrewer, which can be used to choose beautiful colour schemes for plots.

First I loaded in the library into R (it had already been installed):
> library(RColorBrewer)

Then I wanted to find 4 colours between red and yellow: (following an example from here)
> pal(4)
[1] "#FF0000" "#FF5500" "#FFAA00" "#FFFF00"
I decided to use a darker yellow instead of "#FFFF00": #ccc943

Now assign a colour to each of my lines (the start point of which I had stored in a vector 'insertion_start') according to the frequency of data (amount of sequence reads) supporting it (stored in vector 'reads'):
> mycolors <- character(length(insertion_start))
> for (i in 1:length(mycolors))
  {
     myreads <- num_reads[i]
     if (myreads <= 10) { mycolor <- "#CCC943"}
     else if (myreads <= 100) { mycolor <- "#FFAA00"}
     else if (myreads <= 500) { mycolor <- "#FF5500"}
     else                     { mycolor <- "#FF0000"}
     mycolors[i] <- mycolor
  }

Now try plotting with the lines supported by the most reads at the bottom:
> oo <- order(num_reads,decreasing=TRUE)
> plot(insertion_start[oo],yval,ylab="Allele index",xlab="Spanning deletions",col="white")
> segments(insertion_start[oo],yval,insertion_end[oo],yval,col=mycolors[oo],lwd=2)

Et voila, a beautiful plot!
Thank you RColorBrewer : )

No comments: