7

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))
DrDom
  • 4,033
  • 1
  • 21
  • 23
bobfet1
  • 1,603
  • 21
  • 22
  • 1
    I don't think this is going to be very easy; you'll probably have to hack up a way to introduce intermediate points into your segments. http://stackoverflow.com/questions/15924159/smooth-colors-in-geom-line asks a similar question ... – Ben Bolker Jul 19 '13 at 20:08
  • This was implemented in base graphics in the 'plotrix' package. You might try searching to see if anyone has posted a grid-hack in Rhelp. I thought I had seen such an animal in the wild, but I'm not a particularly skilled ggplot-hacker so I'm not in the hunt for this one. – IRTFM Jul 19 '13 at 23:52
  • One hack might be to plot closely spaced points with a color gradient. – IRTFM Jul 20 '13 at 01:07

1 Answers1

6

I was able to achieve this by interpolating a bunch of points in between the start and end points with the point number, and then coloring the segment with a gradient based on the number of the point in between:

example:

# generate data points
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)

# function returns a data frame with interpolated points in between start and end point
interp_points <- function (data) {

  df <- data.frame(line_id=c(),long=c(),lat=c())

  for (i in 1:nrow(data)) { 

    line <- data[i,]

    # interpolate lats and longs in between the two
    longseq <- seq(
                    as.numeric(line["long"]),
                    as.numeric(line["long2"]),
                    as.numeric((line["long2"] - line["long"])/10)
                  )
    latseq <- seq(
                    as.numeric(line["lat"]),
                    as.numeric(line["lat2"]),
                    as.numeric(line["lat2"] - line["lat"])/10
                  )

    for (j in 1:10) {
      df <- rbind(df,data.frame(line_id=i,long=longseq[j],lat=latseq[j],seg_num=j))
    }
  }

  df
}

# run data through function
output <- interp_points(points)

# plot the output
ggplot(output,aes(x=long,y=lat,group=line_id,color=seg_num)) + 
  geom_path(alpha=0.4,size=1) +
  scale_color_gradient(high="red",low="blue")

Unfortunately, when I used real data, the result didn't look as compelling as I was imagining in my mind!

enter image description here

bobfet1
  • 1,603
  • 21
  • 22