[R] smoothScatter function question and adding a legend

Clayton K Collings ccolling at purdue.edu
Fri Jun 10 03:12:46 CEST 2011


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)



More information about the R-help mailing list