tags:

views:

102

answers:

3

Trying to do an xy scatter plot with the z value being denoted by the color of the xy point.

Data:

1.1, 32.27, 19.4  
1.2, 21.34, 18  
1.4, 47.45, 19.4

R code:

 inp <- scan("beps.txt",list(x=0,y=0,z=0))  
 plot(inp$x, inp$y,pch=".")

Creates a great scatter plot, but I would like the points to be colored by the Z value.

+1  A: 

So set the color argument:

 plot(inp$x, inp$y, pch=".", col=inp$z)

Note though that colors are integer-valued.

Dirk Eddelbuettel
Does color the points in the graph, but my z values go from 0-100, and it just looks like static... Off to try ggplot2.
Zac
You can always scale them, or use `cut()` to group.
Dirk Eddelbuettel
One may use RColorBrewer to convert floats to some nice palettes.
mbq
Not really. RColorBrewer defines palettes, it has nothing to do with the scaling.
Dirk Eddelbuettel
+4  A: 

Here is some reproducible example that uses ggplot2. If I understood you correctly I should do what you want.

library(ggplot2)

a = c(1.1, 32.27, 19.4)
b = c(1.2, 21.34, 18)
c = c(1.4, 47.45, 19.4)


df=as.data.frame(rbind(a,b,c))
names(df) = c("x","y","z")
df

p <- ggplot(df, aes(x,y,colour=z)) +geom_point()

In general I strongly recommend ggplot2 for stuff like that. It's really worth learning a little more about. I am still in the middle of the process and realize how much it pays to put some time into ggplot2. If you do not know the package and the documentation, make sure you check it. The documentation is easy to understand and powerful !

ran2
eh. there's no way to be quicker than Dirk and provide a good answer with less keystrokes... but try ggplot2 anyway :)
ran2
A: 

Similar to Dirk's answer, use:

plot(inp$x, inp$y, pch=".", col= heat.colors(30)[inp$z] )

You can, of course, use other color schemes. see ?heat.colors

That seems to work as well and is certainly simpler than ggplot, but ggplot looks prettier. :-)
Zac