Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 color gradient along geom_segment()

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))
like image 765
bobfet1 Avatar asked Jul 19 '13 18:07

bobfet1


1 Answers

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

like image 52
bobfet1 Avatar answered Sep 22 '22 07:09

bobfet1