R には、色を変える矢印を描画する次の関数があります。
require(plotrix)
color.scale.arrow = function(x1,y1,x2,y2,first.col,second.col,
lwd= par('lwd'),lty=par('lty'),angle=30,length=0.25) {
x=mapply(seq,x1,x2,length.out=256) # Each column is one arrow
y=mapply(seq,y1,y2,length.out=256) # Each column is one arrow
arrows(x[255,],y[255,],x[256,],y[256,],
col=ifelse(y[256,]<y[255,],first.col,second.col),
lwd=lwd,lty=lty,angle=angle,length=length)
rgb1=col2rgb(first.col)[,1] / 255
rgb2=col2rgb(second.col)[,1] / 255
cols=rbind(rgb1,(rgb1 + rgb2) / 2,rgb2)
invisible(
sapply(seq(ncol(x)),function(line)
color.scale.lines(x[,line],y[,line],
cols[,'red'],cols[,'green'],cols[,'blue'],
lwd=lwd,lty=lty)
)
)
}
この機能には2つの問題があります..
問題 1:矢印は、上に移動すると赤で始まり青で終わり、下に移動すると青で始まり赤で終わる。実際には、矢印が常に青で始まり、常に赤で終わる必要があります。この問題は、次の単純化されたサンプル データで示されています。
# Create sample data 1
x <- c(5,6,5,6)
y <- c(3,5,5,4)
x1 <- c(5,5)
y1 <- c(3,5)
x2 <- c(6,6)
y2 <- c(5,4)
# Plot sample data 1
plot(x,y, main='')
color.scale.arrow(x1,y1,x2,y2,'red','blue',lwd=2)
次のプロットを作成します。
問題 2:スクリプトは、左から右への矢印のみを許可します。反対方向に矢印を描画しようとすると、エラー メッセージが表示されます。たとえば、このサンプル データをプロットすると、次のようになります。
# Create sample data 2
x <- c(1,3,5,3,2,1,6,2)
y <- c(2,5,3,7,2,1,5,6)
x1 <- c(1,3,5,3)
y1 <- c(2,5,3,7)
x2 <- c(2,1,6,2)
y2 <- c(2,1,5,6)
# Plot sample data 2
plot(x,y, main='')
color.scale.arrow(x1,y1,x2,y2,'red','blue',lwd=2)
次のプロットを取得します。
そして、次のエラーメッセージ:
Error in rgb(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, : color intensity 2, not in [0,1]
これらの問題を解決する方法について何か考えはありますか? よろしくお願いします!