[R] smoothScatter function (color density question) and adding a legend
Mike Marchywka
marchywka at hotmail.com
Mon Jun 13 12:49:14 CEST 2011
----------------------------------------
> Date: Sat, 11 Jun 2011 21:52:21 -0400
> From: ccolling at purdue.edu
> To: r-help at r-project.org
> Subject: [R] smoothScatter function (color density question) and adding a legend
>
> Dear R experts,
>
> I am resending my questions below one more time
> just in case someone out there could help
> but missed my email.
Thanks, I was curious about this and so I installed the geneplot package
and that went fine but I had another issue and need to take care of that.
First, see if "?legend" helps at all ( no idea, I have not used this stuff much).
Also source code should be available for the pacakge you care about.
On quick read, it sounds like this uses a binning system for colors. If you
want something slightly different and much slower, I have some examples
( IIRC) that calculate densities in R using something similar to electric
field calculation around points, ( again I'm in a hurry and pulling this
from archive so caveat emptor this plot may not correspond exactly to
example script below etc),
http://98.129.232.232/coloumb1.pdf
mydensity<-function(x1,x2)
{
len=length(x1);
z<-1:len;
for ( y in 1:len )
{
nx=c(1:(y-1),(y+1):len);
if ( y==1) {nx=2:len;}
if (y==len) {nx=1:(len-1); }
# coloumb is a bit much with overlapping data points, so limite it a bit
z[y]=sum(1.0/(.001*1+(x1[nx]-x1[y])*(x1[nx]-x1[y])+(x2[nx]-x2[y])*(x2[nx]-x2[y])));
#for
}
print(max(z));
print(min(z));
z=z-min(z);
z=100*z/max(z);
hist(z);
color=rainbow(100);
#color=heat.colors(10);
tmap=color[floor(z)+1];
scatterplot3d(x1,x2,z,color=tmap);
plot(x2,x1,col=tmap,cex=.5)
#library("VecStatGraphs2D")
#DrawDensityMap(x1,x2,PaintPoint=TRUE)
#color=terrain.colors(26)
#color=heat.colors(26)
}
>
> I don't think my questions are too hard.
>
> I am most concerned about the transformation function.
> See below.
>
> Thanks,
> Clayton
>
> Hello,
>
> I have a few questions, regarding the smoothScatter function.
>
>
> I have a scatter plot with more than 500,000 data points for
> two samples. So, I am wanting to display the density in colors
> to convince people that my good correlation coefficient is not
> due to an "influential point effect" and plus, I also want to make
> my scatter plot look pretty. Anyway ...
>
> I have been able to make the plot, but I have a couple of questions about it.
> I also want to make a legend for it, which I don't know how to do.
> I have only been playing around with R for a few weeks now off and on.
> So, I am definitely a beginner.
>
>
> 1.
> I have 10 colors for my plot: white representing zero density and dark red representing the maximum density (I presume).
> According to the R documentation, the transformation argument represents the "function mapping the density scale to the color scale."
> Note that in my R code below, transformation = function(x) x^0.14.
> I was wondering how exactly or mathematically that this function relates the density to color.
> I believe the colorRampPalette ramps colors between 0 and 1. I am not sure if x represents the color or the
> density. Since I have 10 colors, I suppose the colorRampPalette would assign values of
> 0, 0.11, 0.22, 0.33, 0.44, 0.55, 0.66, 0.77, 0.88, and 1 for white to dark red. I am not sure though.
> Does anyone know how this works? I am sure its not too too complicated.
>
>
> 2.
> In a related issue, I also would like to make a legend for this plot. Then, I would be able to see the transformation
> function's effects on the color-density relationship. Could someone help me in making a legend for my smoothScatter plot? I would like to place it immediately to the right of my plot as a vertical bar, matching the vertical length of the plot as is often convention.
>
> I really like the smoothScatter function. It is easy to use, and I believe it's relatively new.
>
> Thanks in advance.
>
> -Clayton
>
> > clayton <- c("white", "lightblue", "blue", "darkgreen", "green", "yellow", "orange", "darkorange", "red", "darkred")
> > x <- read.table("c:/users/ccolling/log_u1m1.txt", sep ="\t")
> > smoothScatter(x$V8~x$V7,
> nbin=1000,
> colramp = colorRampPalette(c(clayton)),
> nrpoints=Inf,
> pch="",
> cex=.7,
> transformation = function(x) x^.14,
> col="black",
> main="M1 vs. M3 (r = 0.92)",
> xlab="Normalized M1",
> ylab="Normalized M2",
> cex.axis = 1.75,
> cex.lab = 1.25
> cex.main = 2)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list