适用于许多数据类的R调色板

时间:2012-03-05 08:38:37

标签: r colors ggplot2

R中是否有适合处理许多数据类的调色板包?我有16个顺序数据类。我尝试了RColorBrewer,但它最多有9个数据类,我也试过了rainbow(),但它对顺序数据没有吸引力。感谢

7 个答案:

答案 0 :(得分:18)

经过多次实验,我想出了大部分可以区分的25种颜色。这适用于分类数据,而不是连续/顺序数据。

c25 <- c(
  "dodgerblue2", "#E31A1C", # red
  "green4",
  "#6A3D9A", # purple
  "#FF7F00", # orange
  "black", "gold1",
  "skyblue2", "#FB9A99", # lt pink
  "palegreen2",
  "#CAB2D6", # lt purple
  "#FDBF6F", # lt orange
  "gray70", "khaki2",
  "maroon", "orchid1", "deeppink1", "blue1", "steelblue4",
  "darkturquoise", "green1", "yellow4", "yellow3",
  "darkorange4", "brown"
)
pie(rep(1, 25), col = c25)

答案 1 :(得分:6)

'pals'包装有几种不同的调色板,每种调色板都设计成具有不同的颜色。以下是可用的调色板:

library(pals)
pal.bands(alphabet, alphabet2, cols25, glasbey, kelly, polychrome, 
  stepped, tol, watlington,
  show.names=FALSE)

Discrete color palettes in pals package

答案 2 :(得分:4)

尝试'?colorRampPalette'并制作自己的功能。

答案 3 :(得分:3)

作为Kevin Wright,我发现对我来说最好的是手动调色板。在这里,我贡献了我的调色板以及在多边形中绘制它的方法。

manualcolors<-c('black','forestgreen', 'red2', 'orange', 'cornflowerblue', 
                'magenta', 'darkolivegreen4',  
                'indianred1', 'tan4', 'darkblue', 
                'mediumorchid1','firebrick4',  'yellowgreen', 'lightsalmon', 'tan3',
                "tan1",'darkgray', 'wheat4', '#DDAD4B', 'chartreuse', 'seagreen1',
                'moccasin', 'mediumvioletred', 'seagreen','cadetblue1',
                "darkolivegreen1" ,"tan2" ,   "tomato3" , "#7CE3D8","gainsboro")

# https://stackoverflow.com/questions/15534032/suppress-redraw-when-calling-polygon-in-r
cuts <- function(x)
{
  n <- length(x) %/% 4
  map <- rep(c(rep(TRUE,4),FALSE), n)
  result <- rep(NA, n*5)
  result[map] <- x
  result
}

rownumber<-5
yfloor<-0.5
yheight<-6
faixalist<-c(rep(rownumber,ceiling(length(manualcolors)/rownumber)) )
cro<-list()
sub<-list()
croybot<-list()
croytop<-list()
croxbot<-list()
croxtop<-list()
for (i in 1:length(faixalist)){
  cro[i]<- list(seq(yfloor, yheight, length.out = (faixalist[i]+1)))
  sub[i]<-list(cro[[i]][2]-cro[[i]][1] )
  croybot[i]<-list(cro[[i]][1:(length(cro[[i]])-1)] )
  croytop[i]<-list(croybot[[i]]+sub[[i]])
  croxbot[i]<-list(rep(yfloor+(0.3*(i-1)),faixalist[i]) )
  croxtop[i]<-list(rep(yfloor+(0.3*(i-1))+0.2,faixalist[i]) )
  x <<- cbind(unlist(croxbot),unlist(croxbot),unlist(croxtop),unlist(croxtop) ) 
  y <<- cbind(unlist(croybot),unlist(croytop),unlist(croytop),unlist(croybot))
}
x<-x-0.5
par(mar=c(0,0,0,0), font=2)
plot(0, xlim=c(-0.2,max(x)), ylim=c(min(y),max(y)), ylab = "", xaxt='n', 
     xlab="", yaxt='n',main = NULL)
polygon(x=cuts(t(x)), y=cuts(t(y)), col=manualcolors, lwd=5)
text(x=(t(x[1:length(manualcolors)]+0.1)), y=(t(y[1:length(manualcolors)]+0.5)), 
     labels=manualcolors, cex=0.8, col="white")

enter image description here

答案 4 :(得分:2)

Polychrome 包允许创建具有许多不同颜色的调色板。 一些例子:

# install.packages("Polychrome")
library(Polychrome)

# build-in color palette
Glasbey = glasbey.colors(32)
swatch(Glasbey)


# create your own color palette (36 colors) based on `seedcolors`
P36 = createPalette(36,  c("#ff0000", "#00ff00", "#0000ff"))
swatch(P36)


# create your own color palette (50 colors) based on `seedcolors`
P50 = createPalette(50,  c("#ff0000", "#00ff00", "#0000ff"))
swatch(P50)

答案 5 :(得分:1)

现在有colorspace软件包http://colorspace.r-forge.r-project.org/reference/hcl_palettes.html

例如,创建具有20种不同颜色的调色板:

library(colorspace)
q20 <- qualitative_hcl(20, "Dark2")
demoplot(q20, "bar")

答案 6 :(得分:0)

对于色盲查看器,请查看rcartocolor软件包中的Safe调色板。此调色板包含12种易于区分的颜色。

# install.packages("rcartocolor")
# or
# install.packages("remotes")
# remotes::install_github("Nowosad/rcartocolor")
library(rcartocolor)

nColor <- 12
scales::show_col(carto_pal(nColor, "Safe"))

另一种流行的色盲友好调色板是Okabe Ito scale

示例:

library(ggplot2)
library(rcartocolor)
library(patchwork)
theme_set(theme_classic(base_size = 14) + theme(panel.background = element_rect(fill = "#f6f1eb")))

set.seed(123)
df <- data.frame(x = rep(1:5, 8), 
                 value = sample(1:100, 40), 
                 variable = rep(paste0("category", 1:8), each = 5))

safe_pal <- carto_pal(12, "Safe")

# https://github.com/clauswilke/colorblindr/blob/master/R/palettes.R
palette_OkabeIto <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", 
                      "#0072B2", "#D55E00", "#CC79A7", "#999999")
palette_OkabeIto_black <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", 
                            "#0072B2", "#D55E00", "#CC79A7", "#000000")

# plot
p1 <- ggplot(data = df, aes(x = x, y = value)) + 
  geom_line(aes(colour = variable), size = 1) +
  scale_color_manual(values = palette_OkabeIto_black)


p2 <- ggplot(data = df, aes(x = x, y = value)) + 
  geom_col(aes(fill = variable)) +
  scale_fill_manual(values = safe_pal)

p1 / p2