Skip to content

Scatterplot with the 3rd dimension = color?

15 messages · Kerry, Tal Galili, Duncan Murdoch +3 more

#
I have 3 columns of data and want to plot each row as a point in a
scatter plot and want one column to be represented as a color gradient
(e.g. larger  values being more red). Anyone know the command or
package for this?

Thanks,
KB
#
On 11-10-02 1:11 PM, Kerry wrote:
It's not a particularly effective display, but here's how to do it.  Use 
rainbow(101) in place of rev(heat.colors(101)) if you like.

x <- rnorm(10)
y <- rnorm(10)
z <- rnorm(10)
colors <- rev(heat.colors(101))
zcolor <- colors[(z - min(z))/diff(range(z))*100 + 1]
plot(x,y,col=zcolor)

Duncan Murdoch
#
Yes, perfect! This I can work with.

Thanks,
KB
On Oct 2, 3:55?pm, Duncan Murdoch <murdoch.dun... at gmail.com> wrote:
#
Duncan Murdoch <murdoch.duncan <at> gmail.com> writes:
or

d <- data.frame(x,y,z)
library(ggplot2)
qplot(x,y,colour=z,data=d)

  I agree about the "not particularly effective display"
comment, but if you have two continuous predictors and
a continuous response you've got a tough display problem --
your choices are:

  1. use color, size, or some other graphical characteristic
(pretty far down on the "Cleveland hierarchy")
  2. use a perspective plot (hard to get the right viewing
angle, often confusing)
  3. use coplots/small multiples/faceting (requires
discretizing one dimension)
14 days later
#
Thanks, I consider all of those options and tried some, but the
z=color gradient seems the best option for my data.

kb
On Oct 2, 10:42?pm, Ben Bolker <bbol... at gmail.com> wrote:
#
Oh, I just saw your ggplot example, that works well too. However, it
seems much of the options are hidden for changing the range of colors
or the color types altogether. I'm currently looking through the
ggplot ref manuals.

Thanks,
kb
On Oct 2, 10:42?pm, Ben Bolker <bbol... at gmail.com> wrote:
#
Yes, the qplot works great, but do you know how to allow for multiple
plots? I want one variable to be plotted say from blue to red and
another say from yellow to green but in the same graph, each having
there own separate legends. I've tried print() and arrange() but no
luck.

Thanks again,
kb
On Oct 2, 10:42?pm, Ben Bolker <bbol... at gmail.com> wrote:
#
Here's my loadable data in case it helps. It creates 2 separate plots
which I'd like to be in the same graph with 2 separate legends.

library(ggplot2)

#Here's the 1st plot
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
d <- data.frame(x,y,z)
dg<-qplot(x,y,colour=z,data=d)
dg + scale_colour_gradient(low="red", high="blue")

#Here's the 2nd plot which will delete the 1st plot above but I'd like
them to be plotted together
x1<-rnorm(100)
y2<-rnorm(100)
z3<-rnorm(100)
d1 <- data.frame(x1,y1,z1)
dg1 <-qplot(x1,y1,colour=z1,data=d1)
dg1 + scale_colour_gradient(low="green", high="yellow")

Thanks,
kb
On Oct 2, 10:42?pm, Ben Bolker <bbol... at gmail.com> wrote:
2 days later
#
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:

library(ggplot2)
#Here's the 1st plot
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
d <- data.frame(x,y,z)
dg<-qplot(x,y,colour=z,data=d)
dg + scale_colour_gradient(low="red", high="blue")

#Here's the 2nd plot which will delete the 1st plot above but I'd
like
them to be plotted together
x1<-rnorm(100)
y2<-rnorm(100)
z3<-rnorm(100)
d1 <- data.frame(x1,y1,z1)
dg1 <-qplot(x1,y1,colour=z1,data=d1)
dg1 + scale_colour_gradient(low="green", high="yellow")

I've been trying to get long format working but it just doesn't make
any sense to me.


Thanks,
kb
On Oct 17, 3:10?pm, Kerry <kbro... at gmail.com> wrote:
#
If it would help get any assistance with my issue, here's another
method I'm trying (using R sample data):

ggplot(mtcars, aes(disp)) +
  geom_point(aes(y = mpg, colour = qsec))+
scale_colour_gradient(low="yellow", high="green")+
  geom_point(aes(y = cyl, colour = qsec))+
scale_colour_gradient(low="red", high="blue")

What I want is the var "mpg" to be colored by the var "qsec" from
yellow to green and then the var "cyl" to be colored by the var "qsec"
from red to blue.     Instead, both colors end up being from red to
blue.

Thanks again,
kb
#
AFAIK, you can't 'add' two ggplot2 graphs together; the problem in
this case is that the two color scales would clash. If you're willing
to discretize the z values, then you could pull it off. Here's an
example:

d <- data.frame(x = rnorm(100), y = rnorm(100), z = factor(1 +
(rnorm(100) > 0)))
d1 <- data.frame(x = rnorm(100), y = rnorm(100), z = factor(3 +
(rnorm(100) > 0)))
dd <- rbind(d, d1)

In each data frame, I'm assigning two factor levels depending on
whether z > 0 or not. The factor levels are 1, 2 in d and 3, 4 in d1;
when rbinded together, z has four distinct levels. Now call ggplot():

ggplot(dd, aes(x = x, y = y, colour = z)) + geom_point() +
   scale_colour_manual(values = c('1' = 'red', '2' = 'blue', '3' = 'green',
                                  '4' = 'yellow'))

This may be coarser than you like, so you could always use the cut()
function to discretize z in each data frame; you'll want to assign the
levels so that they are distinct in the combined data frame. Example:

d3 <- data.frame(x = rnorm(100), y = rnorm(100),
                 z = cut(rnorm(100), breaks = c(-Inf, -0.5, 0.5, Inf),
labels = 1:3))
d4 <- data.frame(x = rnorm(100), y = rnorm(100),
                 z = cut(rnorm(100), breaks = c(-Inf, -0.5, 0.5, Inf),
labels = 4:6))
dd2 <- rbind(d3, d4)

mycols <- c('red', 'maroon', 'blue', 'green', 'cyan', 'yellow')
ggplot(dd2, aes(x = x, y = y, colour = z)) + geom_point() +
   scale_colour_manual(breaks = levels(dd2$z),
                       values = mycols)

You can always use the labels = argument of scale_colour_manual() to
assign more evocative legend values, or equivalently, you can assign
the labels in the cut() function within d3 and d4 to those you want in
the legend and leave the plot code as is.

BTW, there is a dedicated ggplot2 list to which you can subscribe
through http://had.co.nz/ggplot2/ (look for the ggplot2 mailing list
near the top of the page). The list archives are accessible through
the same link.

HTH,
Dennis
On Thu, Oct 20, 2011 at 12:25 PM, Kerry <kbrownk at gmail.com> wrote:
#
On 10/21/2011 06:25 AM, Kerry wrote:
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
#
Awesome, thank you so much for this! I plan to play around with this
more next week with my actual data, but it provides a lot more options
than I had before I posted. The link will help too.

kb
On Oct 20, 8:18?pm, Dennis Murphy <djmu... at gmail.com> wrote:
#
Beautiful! It works perfectly, thanks!

kb
On Oct 21, 7:42?am, Jim Lemon <j... at bitwrit.com.au> wrote: