[R] Help with continuous color plot

Jim Lemon jim at bitwrit.com.au
Thu Sep 25 13:59:49 CEST 2014


On Wed, 24 Sep 2014 02:36:58 PM matt at considine.net wrote:
> No, I don't think so.  And I've wondered if I described the problem
> clearly, so I put together the following hack, which seems to be what 
I
> want :
> 
> #create a matrix to hold the values corresponding to various 
percentiles
> vals<-matrix(0,32,21)
> #for each row in the data, collect info on the distribution
> for(i in 1:32){
>    obs <- testdataset[i,]
>    vals[i,] <- quantile(obs, probs=seq(0,1,0.05))
> }
> 
> #pick the last observation to get a distrbution of colors
> cols <- sort(densCols(vals[32,]))
> 
> #set up a blank plot
> matplot(vals, type="n", xlab = 'yrs', ylab = 'Ratio',
>          main = 'Projected ratios')
> 
> #plot confidence bands as polygons, ideally overlaying light to dark
> for (i in 1:10){
>    lines(vals[,i],col=cols[22-i])
>    lines(vals[,22-i],col=cols[22-i])
> 
> polygon(c(seq(1:32),rev(seq(1:32))),c(vals[,22-
i],rev(vals[,i])),col=cols[22
> -i],border="NA") }
> 
> #plot a line for the average case
> lines(vals[,11],col="black")
> 
> If anyone can suggest a more efficient/effective/better/etc/etc way 
of
> doing this, I'd be grateful.  In a nutshell, I am trying to find a
> visually clean way of showing the output of a Monte Carlo analysis.
> 
> Thanks again for everyone's attention.
> Matt
> 
Hi Matt,
That is probably as good a method as any. You could wrap it up as a 
function if you want to do a lot of these. You might want to try 
something like:

library(plotrix)
cols <- color.scale(1:10,extremes=c("green","blue"))

and then use cols[i] for your lines and polygons - it looks better to me.

Jim



More information about the R-help mailing list