随“时间”改变颜色的线图

发布于 2024-12-11 08:28:23 字数 311 浏览 5 评论 0原文

我有一个数据框,其中包含以离散步骤(向上、向下、向左或向右 1 步)移动的随机游走的 xy 坐标。我想绘制路径——由一条线连接的点。当然,这很容易。困难在于这条路径会自我交叉并且变得难以解释。我向点添加抖动以避免过度绘制,但这无助于区分行走的顺序。

我想使用一条线来连接这些点,该线根据类似温度计的色标随“时间”(步骤)改变颜色。

我的随机游走存储在它自己的类中,并且我正在为其编写一个特定的 plot 方法,因此,如果您对如何使用 plot 执行此操作有建议,那么会很棒的。谢谢!

I have a data frame that contains x and y coordinates for a random walk that moves in discrete steps (1 step up, down, left, or right). I'd like to plot the path---the points connected by a line. This is easy, of course. The difficulty is that the path crosses over itself and becomes difficult to interpret. I add jitter to the points to avoid overplotting, but it doesn't help distinguish the ordering of the walk.

I'd like to connect the points using a line that changes color over "time" (steps) according to a thermometer-like color scale.

My random walk is stored in its own class and I'm writing a specific plot method for it, so if you have suggestions for how I can do this using plot, that would be great. Thanks!

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

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

发布评论

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

评论(3

翻身的咸鱼 2024-12-18 08:28:23

这在 ggplot2 中很容易做到:

so <- data.frame(x = 1:10,y = 1:10,col = 1:10)
ggplot(so,aes(x = x, y = y)) + 
    geom_line(aes(group = 1,colour = col))

在此处输入图像描述

This is pretty easy to do in ggplot2:

so <- data.frame(x = 1:10,y = 1:10,col = 1:10)
ggplot(so,aes(x = x, y = y)) + 
    geom_line(aes(group = 1,colour = col))

enter image description here

匿名的好友 2024-12-18 08:28:23

如果您不想使用 ggplot,那么 ?segments 将执行您想要的操作。 -- 我在这里假设 x 和 y 都是时间的函数,正如您的示例中所暗示的那样。

If you prefer not to use ggplot, then ?segments will do what you want. -- I'm assuming here that x and y are both functions of time, as implied in your example.

娇女薄笑 2024-12-18 08:28:23

如果你使用ggplot,你可以设置颜色美感:

library(ggplot2)
walk <-cumsum(rnorm(n=100, mean=0)) 
dat <- data.frame(x = seq_len(length(walk)), y = walk)
ggplot(dat, aes(x,y, colour = x)) + geom_line()

If you use ggplot, you can set the colour aesthetic:

library(ggplot2)
walk <-cumsum(rnorm(n=100, mean=0)) 
dat <- data.frame(x = seq_len(length(walk)), y = walk)
ggplot(dat, aes(x,y, colour = x)) + geom_line()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文