[R] Scatterplot with the 3rd dimension = color?

Jim Lemon jim at bitwrit.com.au
Fri Oct 21 13:42:40 CEST 2011


On 10/21/2011 06:25 AM, Kerry wrote:
> Can someone please help me out with this? The ggplot2 suggestion works
> great but I've spent a few days trying to figure out how to plot 2
> variables with it and I'm stuck. Here's my example code:
> ...

Hi Kerry,
This isn't ggplot2, but it may do what you want.

library(plotrix)
oldmar<-par(mar=c(5,4,4,4))
plot(x,y,type="n")
plotlim<-par("usr")
rect(plotlim[1],plotlim[3],plotlim[2],plotlim[4],col="lightgray")
grid(col="white")
box()
points(x,y,col=color.scale(z,c(1,0),0,c(0,1)),pch=19)
points(x1,y2,col=color.scale(z3,1,c(0,1),0),pch=19)
legendval1<-seq(min(z),max(z),length.out=5)
color.legend(2.9,0.5,3.1,1.5,round(legendval1,1),align="rb",gradient="y",
  rect.col=color.scale(legendval1,c(1,0),0,c(0,1)))
legendval2<-seq(min(z3),max(z3),length.out=5)
color.legend(2.9,-1.5,3.1,-0.5,round(legendval2,1),align="rb",gradient="y",
  rect.col=color.scale(legendval2,c(1,1),c(0,1),0))
par(xpd=TRUE)
text(3,1.6,"z")
text(3,-0.4,"z3")
par(xpd=FALSE,oldmar)

Jim



More information about the R-help mailing list