沿 geom_segment() 的 ggplot2 颜色渐变 [英] ggplot2 color gradient along geom_segment()

查看:41
本文介绍了沿 geom_segment() 的 ggplot2 颜色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组起点和终点坐标,我正在它们之间绘制线段.问题是,我想使用颜色而不是 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.

ggplot2 有没有一种简单的方法可以做到这一点?

Is there a simple way to do this with ggplot2?

示例数据:

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))

推荐答案

我能够通过在起点和终点之间用点号插入一堆点,然后用基于渐变的颜色为线段着色来实现这一点关于中间点的数量:

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:

示例:

# 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!

这篇关于沿 geom_segment() 的 ggplot2 颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆