简体   繁体   English

R xy散点图标记颜色

[英]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. 尝试做一个xy散点图,其中z值由xy点的颜色表示。

Data: 数据:

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

R code: R代码:

 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. 创建一个很好的散点图,但我希望这些点由Z值着色。

So set the color argument: 所以设置颜色参数:

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

Note though that colors are integer-valued. 请注意,颜色是整数值。

Here is some reproducible example that uses ggplot2. 这是一个使用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. 一般来说,我强烈建议ggplot2这样的东西。 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. 我仍处于流程的中间,并意识到花些时间进入ggplot2会花多少钱。 If you do not know the package and the documentation, make sure you check it. 如果您不知道包和文档,请务必检查它。 The documentation is easy to understand and powerful ! 文档易于理解和强大!

Similar to Dirk's answer, use: 与Dirk的答案类似,使用:

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

You can, of course, use other color schemes. 当然,您可以使用其他配色方案。 see ?heat.colors 看?热。颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM