R xy 散点图标记颜色

发布于 2024-09-15 15:49:40 字数 280 浏览 0 评论 0原文

尝试绘制 xy 散点图,其中 z 值由 xy 点的颜色表示。

数据:

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

R 代码:

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

创建一个很棒的散点图,但我希望这些点按 Z 值着色。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-09-22 15:49:40

这是一些使用 ggplot2 的可重现示例。如果我理解正确的话我应该做你想做的事。

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()

一般来说,我强烈推荐 ggplot2 来完成类似的事情。这确实值得我们多学习一点。我仍处于这个过程的中间,意识到在 ggplot2 上投入一些时间是值得的。如果您不知道该软件包和文档,请务必检查一下。该文档易于理解且功能强大!

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 !

征棹 2024-09-22 15:49:40

因此,设置颜色参数:

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

但请注意,颜色是整数值。

So set the color argument:

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

Note though that colors are integer-valued.

情深缘浅 2024-09-22 15:49:40

与德克的答案类似,使用:

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

当然,您可以使用其他配色方案。参见?heat.colors

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文