gpt4 book ai didi

r - R 中的二维颜色渐变图

转载 作者:行者123 更新时间:2023-12-01 22:58:53 26 4
gpt4 key购买 nike

我想生成一个二维颜色渐变矩形,如下图右侧所示。我怎样才能在 R 中做到这一点?使用 colorRamp 或 RColorBrewer 或其他函数/包我可以生成漂亮的一维多洛渐变。但是我该如何在 2D 中执行此操作,包括角落中的多种颜色,例如右上角的矩形?

Color gradients

我想要得到的是例如以下两种渐变类型:

enter image description here enter image description here

BTY:我完全忘了说我找到了上面的图表here (由卢卡·费努制作)。

最佳答案

试试这个:

 m = tcrossprod(sin(seq(0,pi,length=1e2)), cos(seq(0, 3*pi, length=1e2)))
cols = matrix(hcl(h=scales::rescale(m, c(0, 360))), nrow(m))
grid::grid.raster(cols)

您需要找到哪个函数描述您想要的颜色渐变(我使用正弦波进行说明)。

enter image description here

编辑:4个角之间的线性插值

library(grid)
library(scales)

m = tcrossprod(seq(1,2,length=1e2), seq(2, 3, length=1e2))
pal <- gradient_n_pal(c("red","green","yellow","blue"), values = c(2, 3, 4, 6), space = "Lab")
cols = matrix(pal(m), nrow(m))
grid.raster(cols)

enter image description here

编辑2:当函数不可分离时,使用outer,

fun_xy <- function(x, y){

abs(y-x) * abs(y+x)

}

z <- outer(seq(-1,1,length=100), seq(-1,1,length=100), FUN = fun_xy)

cols = matrix(hcl(h=scales::rescale(z, c(0, 200))), nrow(z))
grid::grid.raster(cols)

enter image description here

您还可以直接在函数内部进行颜色混合,而不是随后将值映射到色阶,

fun_xy <- function(x, y){

R <- (x+1)/2
G <- (1-x)/2
B <- (y+1)/2
A <- 1- 0.5*exp(-(x^2+y^2)/0.2)

rgb(R, G, B, A)

}

z <- outer(seq(-1,1,length=100), seq(-1,1,length=100), FUN = fun_xy)

library(grid)
grid.newpage()
grid::grid.raster(z)

enter image description here

关于r - R 中的二维颜色渐变图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com