I have a set of origin and destination coordinates, and I'm drawing line segments in between them. The thing is, I would like to indicate the direction of the line using color instead of the arrows that are provided with geom_segment(). Something like blue transitioning into red, to indicate direction.
Is there a simple way to do this with ggplot2?
Example 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 have been able to color lines differently, but I haven't found a way to have two colors on the same line segment and transition between the two, when I only have a start and end point, with no points in between:
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))