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

2w2cym1i  于 2023-03-15  发布在  其他
关注(0)|答案(7)|浏览(216)

R中有什么好的调色板包可以处理很多数据类吗?我有16个顺序数据类。我试过RColorBrewer,但它最多有9个数据类。我也试过rainbow(),但它对顺序数据没有吸引力。谢谢

ut6juiuv

ut6juiuv1#

经过大量的实验,我想出了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)

nzk0hqpo

nzk0hqpo2#

pals软件包有几个不同的调色板,每个调色板都设计成不同的颜色。

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

sd2nnvve

sd2nnvve3#

作为Kevin Wright,我发现最适合我的方法是手工制作调色板。在这里我贡献了我的调色板和一种用多边形绘制它的方法。

# 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
xfloor    <- 0
yheight   <- 6
widthCol  <- 1
colSpacer <- 0.2
# names in: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
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")

squareVec<-c(rep(rownumber,ceiling(length(manualcolors)/rownumber) ) )

map<-mapybot<-mapytop<-mapxbot<-mapxtop<-numeric()

for (i in 1:length(squareVec)){
    map <- seq(0, 5, length.out = squareVec[i]+1 )
    mapybot <- c(mapybot,(map[1:(length(map)-1)] ) )
    mapytop <- mapybot + (map[2]-map[1] )
    mapxbot <- c(mapxbot,(rep(xfloor + (widthCol*(i-1)), squareVec[i]) ) )
    mapxtop <- c(mapxtop,(rep(xfloor + (widthCol* i ) - colSpacer,squareVec[i]) ) )
}

x <- cbind(mapxbot,mapxbot,mapxtop,mapxtop ) 
y <- cbind(mapybot,mapytop,mapytop,mapybot )

opar<-par(no.readonly=TRUE) # save par

par(mar=c(0,0,0,0), font=2)
plot("", 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=2)

text(x=t(x[1:length(manualcolors)]), 
     y=t(y[1:length(manualcolors)])+(yheight/rownumber)/2, 
     labels= manualcolors, 
     cex=0.8, col="white", pos=4
     )

par(opar) # restore par
}

velaa5lx

velaa5lx4#

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)

cwxwcias

cwxwcias5#

对于色盲的观众,看看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

bf1o4zei

bf1o4zei6#

现在有了colorspace软件包http://colorspace.r-forge.r-project.org/reference/hcl_palettes.html
例如,创建一个包含20种不同颜色的调色板:

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

wmomyfyw

wmomyfyw7#

尝试“?colorRampPalette”并创建自己的函数。

相关问题