为ggplot2生成明亮和黑暗的颜色对 [英] Generate pairs of bright and dark colours for ggplot2
问题描述
brewer.pal(n = 8,name =Paired)
最多可以创建8个颜色对,但只有少数这些颜色适合打印。
是否有更灵活的功能可以产生黑色吊坠?
颜色较深的应该是
有没有一种色彩工具可以解决问题那?
>黑暗(红色)
[1]FF5555
我想出了这个函数,但它只能处理单一颜色,而不能处理矢量。
setbrightness < - function(rgbcolour,brightness)这个函数是一个很好的解决方案, {
##用法:setbrightness(col2rgb(red,0.2)
thiscolour < - rgb2hsv(rgbcolour)
return(hsv(h = thiscolour [1],s = thiscolour [ 2],v = brightness))
}
这是一个可以使用的矢量化函数,其思想是将RGB转换为HSV,其中V对应亮度,然后返回RGB:
library(grDevices)
#r,g,b的每个元素必须在[0,255]
#conv [[3]]的每个元素都必须在[ 0,1],1是最高亮度
亮度< - 函数(r,g,b,因子){
conv < - as.list(as.data.frame(t(rgb2hsv (1,conv,[3]] *因子)
do.call(hsv,conv)
}
#将亮度降低20%
亮度(55,100,150,0.8)
#[1]#2C5078
#增加20%
的亮度(55,100,150,1.2)
#[1]#4278B4
亮度(55,100,150 ,c(0.8,1.2))
#[1]#2C5078#4278B4
x< - rep(LETTERS [1:2],5)
qplot(x = x,geom =bar,fill = x)+
scale_fill_manual(values = brightness(55,100,150,c(0.5,1.5)))
$ b
参见?rgb2hsv
,?hsv
和 wiki 了解更多详情。
编辑:根据您的编辑,似乎您更喜欢使用颜色的名称和亮度的直接值。在这种情况下,矢量化函数看起来非常相似:
#用法:brightness(red,c(0.1,0.3, (rgbcol,v){
conv< - as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
conv [[3]] < - v
do.call(hsv,conv)
}
set.seed(19)
df < - data.frame(a = rlnorm(100),b = 1:10,c = rep(LETTERS [1:10],each = 10))
ggplot(df,aes(x = b, y = a,fill = c))+ geom_area()+ theme_bw()+
scale_fill_manual(values = brightness(red,seq(0.1,0.7,length = 10)))
brewer.pal(n=8,name="Paired")
Can create up to eight colour pairs, but only few of these colours are good for printing. Is there a more flexible function that will generate a dark pendant? The darker one should
- look like the same colour in dark
- well printable
- easy to distinguish from the bright one
Is there a colourbrewer tool that can already solve that?
> dark("red")
[1] "FF5555"
I figured out this function, but it can only handle single colours, not a vector. This function would be a nice solution, if it could be "vectorized".
setbrightness <- function(rgbcolour, brightness) {
## usage: setbrightness(col2rgb("red", 0.2)
thiscolour <- rgb2hsv(rgbcolour)
return(hsv(h=thiscolour[1], s=thiscolour[2], v=brightness))
}
Here is a vectorized function that you can use. The idea is to convert RGB to HSV, where V corresponds to brightness and then go back to RGB:
library(grDevices)
# Every element of r, g, b must be in [0, 255]
# Every element of conv[[3]] must be in [0, 1], 1 is highest brightness
brightness <- function(r, g, b, factor) {
conv <- as.list(as.data.frame(t(rgb2hsv(r, g, b))))
conv[[3]] <- pmin(1, conv[[3]] * factor)
do.call(hsv, conv)
}
# Reducing brightness by 20%
brightness(55, 100, 150, 0.8)
#[1] "#2C5078"
# Increasing by 20%
brightness(55, 100, 150, 1.2)
#[1] "#4278B4"
brightness(55, 100, 150, c(0.8, 1.2))
#[1] "#2C5078" "#4278B4"
x <- rep(LETTERS[1:2], 5)
qplot(x = x, geom = "bar", fill = x) +
scale_fill_manual(values = brightness(55, 100, 150, c(0.5, 1.5)))
See ?rgb2hsv
, ?hsv
and wiki for more details.
Edit: according to your edit it seems that you prefer using names of colours and direct values of brightness. In that case a vectorized function would look very similarly:
# Usage: brightness("red", c(0.1, 0.3, 0.5, 1))
brightness <- function(rgbcol, v) {
conv <- as.list(as.data.frame(t(rgb2hsv(col2rgb(rgbcol)))))
conv[[3]] <- v
do.call(hsv, conv)
}
set.seed(19)
df <- data.frame(a = rlnorm(100), b = 1:10, c = rep(LETTERS[1:10], each = 10))
ggplot(df, aes(x = b, y = a, fill = c)) + geom_area() + theme_bw() +
scale_fill_manual(values = brightness("red", seq(0.1, 0.7, length = 10)))
这篇关于为ggplot2生成明亮和黑暗的颜色对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!