I have a set of origin and destination coordinates, and I draw line segments between them. The fact is, I would like to indicate the direction of the line using color instead of the arrows that are supplied with geom_segment (). Something like blue turning into red to indicate a direction.
Is there an easy way to do this with ggplot2?
Sample data:
points <- data.frame(long=runif(100,-122.4154,-122.3491)) points$lat <- runif(100,37.5976,37.6425) points$long2 <- runif(100,-122.4154,-122.3491) points$lat2 <- runif(100,37.5976,37.6425) # add distance library(geosphere) points$miles <- apply(points, 1, function(x) distHaversine(p1=c(x["long"],x["lat"]),p2=c(x["long2"],x["lat2"]),r=3959))
So far, I could color the lines in different ways, but I have not found a way to have two colors in the same segment of the line and transition between them when I have only the start and end points, without between points:
ggplot(points,aes(x=long,xend=long2,y=lat,yend=lat2,color=miles)) + geom_segment() + scale_color_gradient2(low="red",high="blue",midpoint=median(points$miles))
source share