r中的三向颜色渐变填充

jon*_*jon 22 plot r triplot

如何将三向颜色渐变(热图)填充到三角图(三角图),就像这样.

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

颜色应与triplot平行运行.

Edw*_*ard 23

这是一种方法 - 它有点像黑客,使用点逐个绘制渐变:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

并输出:

在此输入图像描述

您想使用这些渐变来表示数据吗?如果是这样,可能会改变d.red,d.green并且d.blue做到这一点 - 尽管如此,我还没有测试过类似的东西.我希望这有点帮助,但是使用colorRamp例如适当的解决方案可能会更好.

编辑:根据巴蒂斯特的建议,这就是你如何将信息存储在矢量中并一次性绘制所有信息.它要快得多(sm例如,设置为500):

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)
Run Code Online (Sandbox Code Playgroud)

  • 为什么你单独绘制每个点?您可以存储颜色矢量并一次绘制所有点. (2认同)

Bac*_*lin 15

这是一个带有栅格化背景图像的解决方案.该功能的sharpness参数tricol控制颜色渐变为黑色的速度.将它设置为1可以获得Edward的颜色并将其设置为2可以获得下面的颜色.

# Coordinates of the triangle
tri <- rbind(sin(0:2*2/3*pi), cos(0:2*2/3*pi))

# Function for calculating the color of a set of points `pt`
# in relation to the triangle
tricol <- function(pt, sharpness=2){
    require(splancs)
    RGB <- sapply(1:3, function(i){
        a <- sweep(pt, 2, tri[,i])
        b <- apply(tri[,-i], 1, mean) - tri[,i]
        sharpness*((a %*% b) / sum(b^2))-sharpness+1
    })
    RGB[-inpip(pt,t(tri)),] <- 1    # Color points outside the triangle white
    do.call(rgb, unname(as.data.frame(pmin(pmax(RGB, 0), 1))))
}

# Plot
res <- 1000                         # Resolution
xi <- seq(-1, 1, length=res)        # Axis points
yi <- seq(-.8, 1.2, length=res)
x <- xi[1] + cumsum(diff(xi))       # Midpoints between axis points
y <- yi[1] + cumsum(diff(yi))
xy <- matrix(1:(length(x)*length(y)), length(x))
image(xi, yi, xy, col=tricol(as.matrix(expand.grid(x,y))), useRaster=TRUE)
lines(tri[1,c(1:3,1)], tri[2,c(1:3,1)], type="l")
Run Code Online (Sandbox Code Playgroud)

什么tricol()确实是代表每个角落i有一个颜色(红,绿,蓝).它定义了a从角到点的矢量矩阵以及从角到相对边中心的pt矢量b.然后a投影到b并进行缩放以获得相对距离=颜色强度(并应用一个小的黑客sharpness来稍微调整颜色).当涉及到这样的问题时,简单的代数可以起到魔力.

由于锯齿,你会在边缘周围发出微弱的噪音,但是你可以调整它,或者在三角形中绘制略宽的线条. 渐变三角形