Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line plot that changes color over "time"

Tags:

r

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!

like image 225
Charlie Avatar asked Mar 24 '26 10:03

Charlie


1 Answers

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

like image 167
joran Avatar answered Mar 27 '26 01:03

joran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!