如何在 base R 中选择 4-8 种颜色以供色盲人士看到?
下面是基础 R 调色板。在 BASE R 中寻找不使用软件包的解决方案。
Base R 调色板指南: http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
色盲调色板:http://mkweb.bcgsc.ca/colorblind/
具体来说,如何在 BASE R 中手动创建可访问的颜色?
例如“朱红色”、“蓝绿色”和“红紫色”如下纸图所示。
问题中显示的调色板也称为 Okabe-Ito 调色板,如 Okabe & Ito (2008) 建议的那样。从 4.0.0 版本开始,base R 提供了一个新的
palette.colors()
,其中该调色板实际上是默认的:
palette.colors(palette = "Okabe-Ito")
## black orange skyblue bluishgreen yellow
## "#000000" "#E69F00" "#56B4E9" "#009E73" "#F0E442"
## blue vermillion reddishpurple gray
## "#0072B2" "#D55E00" "#CC79A7" "#999999"
除了这个调色板之外,基础 R 中还可以轻松使用各种其他定性调色板。具体来说,新的默认调色板(称为
"R4"
)也被设计为在色觉缺陷下相当强大。请参阅博客文章了解更多详细信息,并参阅后续论文了解自 R 4.0.0 发布以来 Base R 中改进的颜色基础设施的概述:
除了上面的定性调色板之外,自 3.6.0 版本以来,base R 还具有一个新功能
hcl.colors()
,它提供了许多连续和发散的调色板,这些调色板在色觉缺陷下也很强大。它提供了来自 ColorBrewer.org、viridis、CARTO 颜色、Crameri 的科学颜色等的许多调色板的近似值(使用色调-色度-亮度颜色模型导出)。默认是流行的 viridis 调色板。以下博客文章提供了更多详细信息,有关 colorspace
包的论文解释了更多相关/基础工作。
这些是您发布的图像中这些颜色的十六进制代码
colorBlindBlack8 <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7")
pie(rep(1, 8), col = colorBlindBlack8)
colorBlindGrey8 <- c("#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7")
scales::show_col(colorBlindGrey8)
rcartocolor
有 Safe
调色板,有 12 种颜色
safe_colorblind_palette <- c("#88CCEE", "#CC6677", "#DDCC77", "#117733", "#332288", "#AA4499",
"#44AA99", "#999933", "#882255", "#661100", "#6699CC", "#888888")
scales::show_col(safe_colorblind_palette)
如果您需要顺序或发散的色彩图,请检查这些调色板
library(rcartocolor)
display_carto_all(colorblind_friendly = TRUE)
由 reprex 包于 2019-07-22 创建(v0.3.0)
viridis
包中的色标均可供色盲人士使用。 https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html#the-color-scales
如果您需要像链接到的 pdf 中那样按名称使用颜色,请将
viridis
调色板中的颜色与命名的颜色列表相匹配。对于 4-8 岁的孩子来说,这在视觉上很容易做到。
您可以使用包生成调色板,并生成仅使用基础 R 重新创建它们的“硬编码”代码。例如,数据框中所有 colorbrewer 调色板中的 4 种颜色:
# install.packages('RColorBrewer')
palletes <-
structure(c("#A6611A", "#D01C8B", "#7B3294", "#E66101", "#CA0020",
"#CA0020", "#D7191C", "#D7191C", "#D7191C", "#7FC97F", "#1B9E77",
"#A6CEE3", "#FBB4AE", "#B3E2CD", "#E41A1C", "#66C2A5", "#8DD3C7",
"#EFF3FF", "#EDF8FB", "#EDF8FB", "#F0F9E8", "#EDF8E9", "#F7F7F7",
"#FEEDDE", "#FEF0D9", "#F1EEF6", "#F6EFF7", "#F1EEF6", "#F2F0F7",
"#FEEBE2", "#FEE5D9", "#FFFFCC", "#FFFFCC", "#FFFFD4", "#FFFFB2",
"#DFC27D", "#F1B6DA", "#C2A5CF", "#FDB863", "#F4A582", "#F4A582",
"#FDAE61", "#FDAE61", "#FDAE61", "#BEAED4", "#D95F02", "#1F78B4",
"#B3CDE3", "#FDCDAC", "#377EB8", "#FC8D62", "#FFFFB3", "#BDD7E7",
"#B2E2E2", "#B3CDE3", "#BAE4BC", "#BAE4B3", "#CCCCCC", "#FDBE85",
"#FDCC8A", "#BDC9E1", "#BDC9E1", "#D7B5D8", "#CBC9E2", "#FBB4B9",
"#FCAE91", "#C2E699", "#A1DAB4", "#FED98E", "#FECC5C", "#80CDC1",
"#B8E186", "#A6DBA0", "#B2ABD2", "#92C5DE", "#BABABA", "#ABD9E9",
"#A6D96A", "#ABDDA4", "#FDC086", "#7570B3", "#B2DF8A", "#CCEBC5",
"#CBD5E8", "#4DAF4A", "#8DA0CB", "#BEBADA", "#6BAED6", "#66C2A4",
"#8C96C6", "#7BCCC4", "#74C476", "#969696", "#FD8D3C", "#FC8D59",
"#74A9CF", "#67A9CF", "#DF65B0", "#9E9AC8", "#F768A1", "#FB6A4A",
"#78C679", "#41B6C4", "#FE9929", "#FD8D3C", "#018571", "#4DAC26",
"#008837", "#5E3C99", "#0571B0", "#404040", "#2C7BB6", "#1A9641",
"#2B83BA", "#FFFF99", "#E7298A", "#33A02C", "#DECBE4", "#F4CAE4",
"#984EA3", "#E78AC3", "#FB8072", "#2171B5", "#238B45", "#88419D",
"#2B8CBE", "#238B45", "#525252", "#D94701", "#D7301F", "#0570B0",
"#02818A", "#CE1256", "#6A51A3", "#AE017E", "#CB181D", "#238443",
"#225EA8", "#CC4C02", "#E31A1C"), .Dim = c(35L, 4L), .Dimnames = list(
c("BrBG", "PiYG", "PRGn", "PuOr", "RdBu", "RdGy", "RdYlBu",
"RdYlGn", "Spectral", "Accent", "Dark2", "Paired", "Pastel1",
"Pastel2", "Set1", "Set2", "Set3", "Blues", "BuGn", "BuPu",
"GnBu", "Greens", "Greys", "Oranges", "OrRd", "PuBu", "PuBuGn",
"PuRd", "Purples", "RdPu", "Reds", "YlGn", "YlGnBu", "YlOrBr",
"YlOrRd"), NULL))
这是用于生成此代码的代码:
number_of_colors <- 4
pallete_names <- rownames(RColorBrewer::brewer.pal.info)
pallete_color_generator <- RColorBrewer::brewer.pal
# make color hexcodes:
palletes<-lapply(pallete_names,
pallete_color_generator,
n = number_of_colors)
# turn into matrix:
palletes<-do.call(rbind,palletes)
rownames(palletes)<-pallete_names
# dump hard-coded R code to create the object to the console:
dump('palletes','')
要在基础中创建自定义调色板,如问题图中所列:
customvermillion<-rgb(213/255,94/255,0/255)
custombluegreen<-rgb(0/255,158/255,115/255)
customblue<-rgb(0/255,114/255,178/255)
customskyblue<-rgb(86/255,180/255,233/255)
customreddishpurple<-rgb(204/255,121/255,167/255)
然后在
中引用时plot()
而不是使用参数:
plot(mtcars$mpg,mtcars$hp,col=c("orange","skyblue"))
使用参数:
plot(mtcars$mpg,mtcars$hp,col=c(customorange,customskyblue))