我試圖繪製矢量場geom_segment不改變大小,一些載體設置爲不可見,使用此代碼:GGPLOT2:如預期
library(cowplot)
size <- 1/12
strength <- 8
centerx <- 0
centery <- -12
diagsize <- 200
x <- rep(1:diagsize - diagsize/2, times = diagsize)
y <- rep(1:diagsize - diagsize/2, each = diagsize)
xend <- numeric()
yend <- numeric()
distance <- numeric()
replace_factor_01 <- numeric()
replace_factor <- numeric()
h <- numeric()
for (i in 1:(diagsize*diagsize)){ #
distance[i] <- sqrt((x[i]-centerx)^2+(y[i]-centery)^2) # simply get absolute distance of each pixel to lens center
replace_factor_01[i] <- 0 # this will be color intensity
replace_factor[i] <- 0 # wheras this will be arrow length
h[i] <- 0 # this will be color (0 = red, 0.667 = blue)
if (distance[i] < 2*3.141592/size){replace_factor_01[i] <- sin(distance[i]*size)} # if within range, compute distortion as value -1..1
replace_factor[i] <- replace_factor_01[i]*strength # linearly stretch
if (replace_factor[i] < 0){h[i] <- 0.667} # set color
xend[i] <- x[i] + (x[i]-centerx)/distance[i]*replace_factor[i] # compute distortion vector
yend[i] <- y[i] + (y[i]-centery)/distance[i]*replace_factor[i]
if ((x[i] %% 5) !=0 | (y[i] %% 5) != 0) {replace_factor_01[i] <- 0} # only set some arrows visible
}
data <- data.frame(x,y,distance, h, replace_factor_01,replace_factor,xend,yend)
p <- ggplot(data,aes(x,y))+
geom_segment(aes(xend=xend,yend=yend),col=hsv(h,abs(replace_factor_01),1))
#geom_segment(aes(xend=xend,yend=yend),col=hsv(h,abs(replace_factor_01),1), size=5)
print(p)
結果看起來是這樣的:
當我使用「大小= 5」,該行不只是變得更厚,但像這樣:
我發生了什麼問題?
請提供一些示例數據與'dput()'。 – Mako212
請參閱[在R中創建可重現示例](https://stackoverflow.com/a/5963610/4421870) – Mako212
好的,抱歉,我認爲這個錯誤可能很明顯。我現在包含完整可重複的代碼。 –