如何用R(使用独立类别/多维度) [英] how to color a qualitative gradient with R (using independent categories / multiple dimensions)
问题描述
对不起,如果我缺少关于 colorRampPalette
和 brewer.pal
工作的基本知识,基于多个变量创建定性颜色渐变?我的目标是创建一个多维定性渐变,如下图所示(红到绿,红到黄,红到蓝和不红到黄到绿-to-blue)。
sorry if i'm missing something fundamental about how colorRampPalette
and brewer.pal
work, but how can you create a qualitative color gradient based on multiple variables? my goal is to create a multidimensional qualitative gradient like the image shown below (red-to-green, red-to-yellow, red-to-blue and not red-to-yellow-to-green-to-blue).
最小可重复范例:
minimal a reproducible example:
# example data set
mydata <-
data.frame(
v = runif( 100 ) ,
w = runif( 100 ) ,
x = runif( 100 ) ,
y = runif( 100 ) ,
z = runif( 100 )
)
# five columns of random values between zero and one
# use three columns with the `rgb` function to find
# the color between red, green, and blue using all three values
three.dimensions <- rgb( mydata$x , mydata$y , mydata$z )
# between zero and one, this gives black and white
plot( 0:1 , 0:1 , col = rgb( 0:1 , 0:1 , 0:1 ) , pch = 16 , cex = 3 )
# using the three sets of values, color on an rgb gradient
plot( rnorm( 100 ) , rnorm( 100 ) , col = three.dimensions , pch = 16 , cex = 3 )
# how is this multi-dimensionality
# supposed to be implemented in RColorBrewer?
library(RColorBrewer)
# create two colorRampPalette functions,
# for three- and five-category qualitative data
mypal3 <- brewer.pal( 3 , "Set1" )
mypal5 <- brewer.pal( 5 , "Set1" )
crp3 <- colorRampPalette( mypal3 )
crp5 <- colorRampPalette( mypal5 )
# this is just linear.. red, slowly to blue, slowly to green.
plot( rep( 1:10 , 10 ) , rep( 1:10 , each = 10 ) , col = crp3(100)[1:100] , pch = 16 , cex = 3 )
# how would i use RColorBrewer to get a three-way gradient
# with shades between red and green, as opposed to just red-to-blue-to-green?
# this is also linear across five colors.
plot( rep( 1:10 , 10 ) , rep( 1:10 , each = 10 ) , col = crp5(100)[1:100] , pch = 16 , cex = 3 )
# how would i use RColorBrewer to get a five-way gradient
# with shades between
# red and blue
# red and green
# red and purple
# red and orange
# and every other combo, depending on the values in `mydata`?
推荐答案
如果我理解问题,颜色缩放到不同的组。这是一个可能的策略,
If I understand the question, you want to map different colour scales to different groups. Here's a possible strategy,
x <- seq(0,6*pi-0.01, length=100)
y <- sin(x)
i <- x%/%pi + 1 # groups in the data
d <- data.frame(x=x,y=y,i=i)
cols <- RColorBrewer::brewer.pal(length(unique(i)),"Set1")
library(plyr)
# for each group, map to a specific colorRamp
d2 <- ddply(d, "i", function(.d){
id <- as.numeric(as.character(unique(.d$i)))
pal <- colorRamp(c(cols[id], "white"), )
cols <- pal(scales::rescale(.d$y))
mutate(.d, col=rgb(cols[,1],cols[,2],cols[,3], maxColorValue = 255))
})
ggplot(d2, aes(x,y,colour=col,group=i))+
geom_line(lwd=5) + scale_colour_identity() +
theme_minimal()
相同的想法将适用于地图。
same idea would apply for a map.
这篇关于如何用R(使用独立类别/多维度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!