开发者

R xy scatter plot marker color

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 p开发者_如何学编程lot, but I would like the points to be colored by the Z value.


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 !


So set the color argument:

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

Note though that colors are integer-valued.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜