如何在ggplot2中显示曲线的方向? [英] How do I show the orientation of a curve in ggplot2?

查看:114
本文介绍了如何在ggplot2中显示曲线的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个参数化轮廓,我正在绘制 R 。我想要做的是沿着曲线添加箭头,以向观众显示曲线走向的方向。



这里是我用来生成曲线:

 库(ggplot2)
库(网格)
set.seed(9)$ b (t)=(runif(2 ^ 12,min = 2 ^ -5,max = 16))

U <-function(t)exp(4 * log(t)-4 (t(t))*(cos(log(t)+ 3 * t))
#Re(t ^(4 + 1i)* t)* exp( - (4-3i)* t) (t)(4 + 1i)×(sin(log(t)+ 3 * t)) t)* exp( - (4-3i)* t))


X <-sapply(T,U)
Y <-sapply(T,V)

df <-data.frame(X = X,Y = Y)

p <-ggplot(data = df,aes(x = df $ X,y = df $ y))

p + theme_bw()+
geom_path(size = 1,color ='blue',linetype = 1)#+
#geom_segment(aes(xend =(尾(X,n = -1),NA),yend = c(尾(Y,n = -1),NA)),
# cm)),color ='blue')
dev.off()

我评论过的最后一部分:

 #+ 
#geom_segment(aes(xend = c(tail(X,n = -1),NA),yend = c(tail(Y,n = -1),NA)),
#arrow = arrow =单位(0.2,cm)),color ='blue')

我想要什么,但是箭头非常接近,曲线最终看起来是模糊而不是定向。

这里是曲线的模糊和非模糊版本:





,例如



  library(ggplot2)
library(grid)
set.seed(9)
T < - sort(runif(2 ^ 12,min = 2 ^ -5 ,max = 16))
U < - 函数(t)exp(4 * log(t)-4 * t)*(cos(log(t)+ 3 * t))
V < - 函数(t)exp(4 * log(t)-4 * t)*(sin(log(t)+ 3 * t))
drough< - data.frame(x = sapply T,U),y = sapply(T,V))


p < - ggplot(data = drough,aes(x = x,y = y))+
geom_path()

##因为参数曲线是以不均匀间距生成的
##我们可以尝试沿着路径更均匀地重新采样
parametric_smoothie< - function(x ,y,N = 1e2,相位= 1,偏移= 0){

长度<-c(0,sqrt(diff(x)^ 2 + diff(y)^ 2))$ (相位* lmax / N,lmax-相位* lmax / N,长度= 0°),其中, N)+ (l,x,newpos)$ y
yy < - approx(l,y,newpos)$ y
data.frame(x = xx,y = yy)
}

##这是一组更精细的点
dfine< - parametric_smoothie(X,Y,20)

gridExtra :: grid.arrange(p + geom_point(data = drough,col =gray),
p + geom_point(data = dfine,col =gray),ncol = 2)

  ##现在我们使用这个函数为箭头
##创建N个起点,另一个N端稍微分开以给出方向感
relay_arrow < - function(x,y,N = 10,phase = 0.8,offset = 1e-2,...){

start < - parametric_smoothie(x,y,N,phase )
end< - parametric_smoothie(x,y,N,phase,offset)

data.frame(xstart = start $ x,xend = end $ x,
ystart = start $ y,yend = end $ y)

}

break < - relay_arrow(drough $ x,drough $ y,N = 20)

p + geom_point(data = breaks,aes(xstart,ystart),col =grey98,size = 2)+
geom_segment(data = breaks,aes(xstart, ystart,xend = xend,yend = yend),
arrow = arrow(length = unit(0.5,line)),
col =red,lwd = 1)


I have a parameterized contour that I'm plotting in R. What I'm trying to do is add arrows along the curve to show the viewer which direction the curve is going in.

Here's the code I'm using to generate the curve:

library(ggplot2)
library(grid)
set.seed(9)
T<-sort(runif(2^12,min=2^-5, max=16))

U<-function(t) exp(4*log(t) - 4*t)*(cos(log(t) + 3*t))
#Re(t^(4+1i)*t)*exp(-(4-3i)*t))
V<-function(t) exp(4*log(t) - 4*t)*(sin(log(t) + 3*t)) 
#Im(t^(4+1i)*t)*exp(-(4-3i)*t))


X<-sapply(T,U)
Y<-sapply(T,V)

df<-data.frame(X=X,Y=Y)

p<-ggplot(data=df,aes(x = df$X, y = df$Y))

p+theme_bw()+
geom_path(size=1,color='blue',linetype=1) #+
#geom_segment(aes(xend=c(tail(X, n=-1), NA), yend=c(tail(Y, n=-1), NA)),
#arrow=arrow(length=unit(0.2,"cm")),color='blue')
dev.off()

The last part I commented out:

#+
#geom_segment(aes(xend=c(tail(X, n=-1), NA), yend=c(tail(Y, n=-1), NA)),
#arrow=arrow(length=unit(0.2,"cm")),color='blue') 

does something similar to what I want, but the arrows are very close together and the curve ends up looking "fuzzy" rather than directed.

Here's the fuzzy and non-fuzzy version of the curve:

Thank you!

解决方案

It might look better if the arrows were more equally spaced along the curved path, e.g.

library(ggplot2)
library(grid)
set.seed(9)
T <- sort(runif(2^12,min=2^-5, max=16))
U <- function(t) exp(4*log(t) - 4*t)*(cos(log(t) + 3*t))
V <- function(t) exp(4*log(t) - 4*t)*(sin(log(t) + 3*t)) 
drough <- data.frame(x=sapply(T,U), y=sapply(T,V))


p <- ggplot(data = drough, aes(x = x, y = y))  + 
       geom_path() 

## because the parametric curve was generated with uneven spacing
## we can try to resample more evenly along the path
parametric_smoothie <- function(x, y, N=1e2, phase=1, offset=0) {

  lengths <- c(0, sqrt(diff(x)^2 + diff(y)^2))
  l <- cumsum(lengths)
  lmax <- max(l)
  newpos <- seq(phase*lmax/N, lmax-phase*lmax/N, length.out = N) + offset*lmax/N
  xx <- approx(l, x, newpos)$y
  yy <- approx(l, y, newpos)$y
  data.frame(x = xx, y = yy)
}

## this is a finer set of points
dfine <- parametric_smoothie(X, Y, 20)

gridExtra::grid.arrange(p + geom_point(data = drough, col="grey"),
                        p + geom_point(data = dfine, col="grey"), ncol=2)

## now we use this function to create N start points for the arrows
## and another N end points slightly apart to give a sense of direction
relay_arrow <- function(x, y, N=10, phase = 0.8, offset = 1e-2, ...){

  start <- parametric_smoothie(x, y, N, phase)
  end <- parametric_smoothie(x, y, N, phase, offset)

  data.frame(xstart = start$x, xend = end$x, 
             ystart = start$y, yend = end$y)

}

breaks <- relay_arrow(drough$x, drough$y, N=20)

p + geom_point(data = breaks, aes(xstart, ystart), col="grey98", size=2) +
  geom_segment(data = breaks, aes(xstart, ystart, xend = xend, yend = yend), 
               arrow = arrow(length = unit(0.5, "line")),
               col="red", lwd=1)

这篇关于如何在ggplot2中显示曲线的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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