2016-12-02 65 views
2

我有我繪製在R參數化的輪廓。我正在試圖做的是添加箭頭沿着曲線顯示哪個方向的曲線在會觀衆如何在ggplot2中顯示曲線的方向?

下面是我使用生成曲線代碼:

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

最後第一部分註釋掉:

#+ 
#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') 

做類似我想要的東西,但箭頭是非常接近的,曲線最終看起來「模糊」,而不是導演。

這裏是曲線的模糊和非模糊的版本:

Fuzzy Curve

enter image description here

謝謝!

回答

6

它看起來更好,如果箭頭爲more equally spaced along the curved path,如

enter image description here

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) 

enter image description here

## 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) 
+0

這種「動態坡」的想法很聰明!如果你對它更加評論,我會接受,所以像我這樣的小白就能理解它。 – GFauxPas

+0

非常感謝! – GFauxPas

2

一種方式來做到這一點是後畫他們。你也許可以得到方向通過使用角度的審美更好(如果它是很容易摸出):

p<-ggplot(data=df,aes(x = X, y = Y)) 
p + 
    geom_path(size=1,color='blue',linetype=1)+ 
    geom_segment(data = df[seq(1, nrow(df), 20), ], aes(x = X, y = Y, xend=c(tail(X, n=-1), NA), yend=c(tail(Y, n=-1), NA)), 
    arrow=arrow(length=unit(0.2,"cm"), type = "closed"), color="blue", linetype = 0, inherit.aes = FALSE) 

enter image description here

注意封閉的箭頭類型。我不得不這樣做,所以他們不被解釋爲線條,因此當linetype = 0消失。

1

與你的代碼稍加修改試試這個(你不想通過具有點的數量較少妥協曲線的質量,並在同一時間,你想有較小的段數來繪製箭頭更好箭頭的質量):

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) 
df1 <- df[seq(1,length(X), 8),] 

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

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

enter image description here

+0

的箭頭是在這一個很好的,但有一種方法,使加盟箭頭無形的線?這絕對是一個改進!謝謝! – GFauxPas

+0

可以是可以嘗試DF1 < - DF [SEQ(1,長度(X),7),]或DF1 < - DF [SEQ(1,長度(X),6),],段會更難以區分從曲線中減少seq()的'by'參數,但同時箭頭會更接近。 –

+1

@GFauxPas你可以試試我做的線型的技術= 0 –

相關問題