n种颜色的渐变,范围从颜色1和颜色2 [英] Gradient of n colors ranging from color 1 and color 2
问题描述
我经常使用ggplot2
来改善渐变效果(点击此处查看示例).我需要在基础上工作,我认为scales
也可以在其中使用以创建颜色渐变,但是我对如何做到这一点很不满意.基本目标是生成n种颜色的调色板,范围从x颜色到y颜色.该解决方案需要在基础上工作.这是一个起点,但没有输入n的位置.
I often work with ggplot2
that makes gradients nice (click here for an example). I have a need to work in base and I think scales
can be used there to create color gradients as well but I'm severely off the mark on how. The basic goal is generate a palette of n colors that ranges from x color to y color. The solution needs to work in base though. This was a starting point but there's no place to input an n.
scale_colour_gradientn(colours=c("red", "blue"))
我很清楚:
brewer.pal(8, "Spectral")
来自RColorBrewer
的
.我正在寻找更多类似于ggplot2
处理渐变的方法,该方法表示我有这两种颜色,并且一路上需要15种颜色.我该怎么办?
from RColorBrewer
. I'm looking more for the approach similar to how ggplot2
handles gradients that says I have these two colors and I want 15 colors along the way. How can I do that?
推荐答案
colorRampPalette
在这里可能是您的朋友:
colorRampPalette
could be your friend here:
colfunc <- colorRampPalette(c("black", "white"))
colfunc(10)
# [1] "#000000" "#1C1C1C" "#383838" "#555555" "#717171" "#8D8D8D" "#AAAAAA"
# [8] "#C6C6C6" "#E2E2E2" "#FFFFFF"
并且为了展示它的工作原理:
And just to show it works:
plot(rep(1,10),col=colfunc(10),pch=19,cex=3)
这篇关于n种颜色的渐变,范围从颜色1和颜色2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!