[R] adding color bar to a graph - one possible solution
pisicandru at hotmail.com
Fri Aug 7 16:36:47 CEST 2009
I think i get one solution, although the legend color bar is horizontal rather than vertical.
I've used the "image.plot" command and i defined the colors with the command "designer.colors" from the package fields.
The problem with the vertical color bar is that the bar label (in my case "Density") is written vertically (that means parallel with the vertical color bar) and i wanted it horizontal (and perpendicular to the bar on top of it - like on map legends - if my explanation is clear enough).
The graph is not self explanatory at a first glance since the correlation ellipsoids are for the scatter plot but on the graph there is a scatter density plot, so the colors correspond to the densities and not the values themselves .... for this reason i needed a label for the color, if this makes sense. I would like to have the label "Density" somewhere else and maybe with a bigger font, and i suspect that the argument "legend.args" would help, but i don't quite understand how to use it in the function so .... maybe somebody else has an idea.
Meanwhile my solution is at: ftp://ftpext.usgs.gov/pub/er/fl/st.petersburg/Monica_pal/
and the file name is: robcor_training_pca_legend.pdf
My code for the color bar is:
col1 <- designer.colors( n=256, col= c("darkviolet", "deepskyblue4", "green", "yellow", "red"), x= seq(0,1, 0.25))
image.plot( legend.only=TRUE, zlim= c(0, round(zmax, 2)), col = col1, legend.lab = "Density", horizontal = TRUE)
Thanks for all the help again,
> Date: Fri, 7 Aug 2009 21:11:46 +1000
> From: jim at bitwrit.com.au
> To: pisicandru at hotmail.com
> CC: r-help at stat.math.ethz.ch
> Subject: Re: [R] adding color bar to a graph
> Monica Pisica wrote:
>> Hi everybody,
>> I am wondering how i can add a "stretch" color bar / legend to a graph that uses colorBrewer to define the colors in it. I will try to explain my graph, but i also uploaded it at: ftp://ftpext.usgs.gov/pub/er/fl/st.petersburg/Monica_pal/
>> The file is: robcor_training_pca.pdf - i will also attach this file in case some people accept attachments.
>> So i took apart the cor.plot function from mvoutlier and i used instead of the scatter plot function the smooth scatter density plot from geneplotter, generating my own function called cordens.plot. Now i would like to add a colorbar on the right side of the graph with red as my highest density and purple as the lowest density and right next to the colors labels "min" for purple and "max" for red (i don't want actual numbers, although i suppose i can deal with that as well if need be).
>> I've tried image.plot function but i cannot set correctly the colors in the bar since i am not using an "orthodox" rainbow set. Originally my colors in the graph are set with colorRampPalette(c("darkviolet", "deepskyblue4", "green", "yellow", "red")).
> Hi Monica,
> I think color.legend in the plotrix package will do what you want.
> and you will have to add some space on the right side
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 738312 bytes
Desc: not available
More information about the R-help