问题描述
我如何像这样将三向颜色渐变(热图)填充到三坐标图(三角形图)中.
How can I fill three way color gradient (heatmap) to a triplot (triangle plot), like this.
plot(NA,NA,xlim=c(0,1),ylim=c(0,sqrt(3)/2),asp=1,bty="n",axes=F,xlab="",ylab="")
segments(0,0,0.5,sqrt(3)/2)
segments(0.5,sqrt(3)/2,1,0)
segments(1,0,0,0)
颜色应与三线平行.
推荐答案
这是一种实现方法-有点破烂,使用点来逐段绘制渐变:
Here is one way to do it - it's a bit of a hack, using points to plot the gradient piece by piece:
plot(NA,NA,xlim=c(0,1),ylim=c(0,1),asp=1,bty="n",axes=F,xlab="",ylab="")
segments(0,0,0.5,sqrt(3)/2)
segments(0.5,sqrt(3)/2,1,0)
segments(1,0,0,0)
# sm - how smooth the plot is. Higher values will plot very slowly
sm <- 500
for (y in 1:(sm*sqrt(3)/2)/sm){
for (x in (y*sm/sqrt(3)):(sm-y*sm/sqrt(3))/sm){
## distance from base line:
d.red = y
## distance from line y = sqrt(3) * x:
d.green = abs(sqrt(3) * x - y) / sqrt(3 + 1)
## distance from line y = - sqrt(3) * x + sqrt(3):
d.blue = abs(- sqrt(3) * x - y + sqrt(3)) / sqrt(3 + 1)
points(x, y, col=rgb(1-d.red,1 - d.green,1 - d.blue), pch=19)
}
}
输出:
您是否要使用这些渐变来表示数据?如果是这样,可以更改d.red
,d.green
和d.blue
来做到这一点-尽管我还没有测试过类似的东西.我希望这会有所帮助,但是例如使用colorRamp
的正确解决方案可能会更好.
Did you want to use these gradients to represent data? If so, it may be possible to alter d.red
, d.green
, and d.blue
to do it - I haven't tested anything like that yet though. I hope this is somewhat helpful, but a proper solution using colorRamp
, for example, will probably be better.
编辑:根据baptiste的建议,这是将信息存储在向量中并一次绘制所有内容的方式.速度要快得多(例如,特别是sm
设置为500):
EDIT: As per baptiste's suggestion, this is how you would store the information in vectors and plot it all at once. It is considerably faster (especially with sm
set to 500, for example):
plot(NA,NA,xlim=c(0,1),ylim=c(0,1),asp=1,bty="n",axes=F,xlab="",ylab="")
sm <- 500
x <- do.call(c, sapply(1:(sm*sqrt(3)/2)/sm,
function(i) (i*sm/sqrt(3)):(sm-i*sm/sqrt(3))/sm))
y <- do.call(c, sapply(1:(sm*sqrt(3)/2)/sm,
function(i) rep(i, length((i*sm/sqrt(3)):(sm-i*sm/sqrt(3))))))
d.red = y
d.green = abs(sqrt(3) * x - y) / sqrt(3 + 1)
d.blue = abs(- sqrt(3) * x - y + sqrt(3)) / sqrt(3 + 1)
points(x, y, col=rgb(1-d.red,1 - d.green,1 - d.blue), pch=19)
这篇关于三向色渐变填充r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!