I am looking to make a colored triangle for my plot with a customized color gradient starting from each vertex of the triangle. Example:
Below is my code so far. The colors are correct but I am unable to have each of the preferred color start from the vertex. The triangle currently looks like that:
My current code:
## make a triangle of cell coordinates
cells.x<-cells.y<-vector()
k<-1
asp<-abs(diff(par()$usr[4:3])/diff(par()$usr[2:1]))*par()$pin[2]/par()$pin[1]
for(i in 1:50){
for(j in i:(100-i+1)){
cells.x[k]<-0.1+(j-1)*0.1/100
cells.y[k]<-(-1)+(i-1)*2*0.1*asp/100
k<-k+1
}
}
# Define the three colors for the gradient
color1 <- "#1e88e5"
color2 <- "#d81b60"
color3 <- "#ffc107"
# Create a color gradient function between the three colors
color_gradient <- colorRampPalette(c(color1, color2, color3))
# Calculate the number of points
num_points <- length(cells.x)
# Generate the gradient colors based on the number of points
gradient_colors <- color_gradient(num_points)
## draw points
dx<-diff(range(cells.x))
dy<-diff(range(cells.y))
red.x<-max(cells.x)
red.y<-min(cells.y)
red<-1-sqrt((((cells.x-red.x)/dx)^2+((cells.y-red.y)/dy)^2)/2)
green.x<-min(cells.x)
green.y<-min(cells.y)
green<-1-sqrt((((cells.x-green.x)/dx)^2+((cells.y-green.y)/dy)^2)/2)
blue.x<-min(cells.x)
blue.y<-max(cells.y)
blue<-1-sqrt((((cells.x-blue.x)/dx)^2+((cells.y-blue.y)/dy)^2)/2)
points(cells.x,cells.y,col=gradient_colors, pch=15,cex=0.5)