[英]ggplot2 with colour blind ggthemes except black
I want to create a general line of code I can apply to any ggplot<\/code> grouped barchart I make.
我想创建一个通用代码行,我可以将其应用于我制作的任何
ggplot<\/code>分组条形图。
I want it to make my graphs colour blind friendly.我希望它使我的图表对色盲友好。 In the library
ggthemes<\/code> , the
scale_fill_colorblind<\/code> function does just the job.
在库
ggthemes<\/code>中,
scale_fill_colorblind<\/code>函数就完成了这项工作。
My problem is that black is often picked as one of the colours;我的问题是黑色经常被选为其中一种颜色。 I sometimes need to overlay confidence intervals and other stuff, so black is not really an option.我有时需要叠加置信区间和其他东西,所以黑色并不是真正的选择。
library(ggplot2)
library(ggthemes)
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")+
scale_fill_colorblind()
at best a hack, 充其量只是一个hack,
ggthemes_data$colorblind <- ggthemes_data$colorblind[-1]
assignInNamespace("ggthemes_data", ggthemes_data, ns="ggthemes")
last_plot() + scale_fill_colorblind()
Another option is use scale_fill_manual
with ggthemes::colorblind_pal
. 另一个选择是将scale_fill_manual
与ggthemes::colorblind_pal
scale_fill_manual
一起使用。
library(ggplot2)
library(ggthemes)
ggplot(diamonds, aes(clarity, fill = cut)) +
geom_bar(position = "dodge") +
scale_fill_colorblind()
n <- length(unique(diamonds$cut))
# or
n <- data.table::uniqueN(diamonds$cut)
# Manually fill the geom with breaks provided by the data
# and getting one extra color from ggthemes::colorblind_pal
# then dropping black (the first color)
ggplot(diamonds, aes(clarity, fill = cut)) +
geom_bar(position = "dodge") +
scale_fill_manual(breaks = diamonds$cut,
values = colorblind_pal()(n + 1)[-1])
This answer builds on the answer from Alec but creates a function that returns scale_fill_discrete<\/code> with a type argument equal to a vector of the last 7 colors used by
scale_fill_colorblind<\/code> .
此答案基于 Alec 的答案,但创建了一个函数,该函数返回
scale_fill_discrete<\/code> ,其类型参数等于
scale_fill_colorblind<\/code>使用的最后 7 种颜色的向量。
You do not have to specify the number of groups in your data;您不必指定数据中的组数; if there are N groups,
scale_fill_discrete<\/code> will use the first N colors in its
type<\/code> argument.
如果有 N 个组,
scale_fill_discrete<\/code>将在其
type<\/code>参数中使用前 N 个颜色。
The function I wrote will also let you select which colors you want, so you can drop black or use the 2nd, 4th, and 5th colors.我写的函数还可以让你选择你想要的颜色,所以你可以去掉黑色或使用第 2、第 4 和第 5 种颜色。
Load packages加载包<\/h1>
library(ggplot2) # Included because this is what I used to make the images: theme_set(theme_minimal()) library(ggthemes)<\/code><\/pre> Define color\/fill functions定义颜色\/填充函数<\/h1># Fill scale_fill_colorblind7 = function(.ColorList = 2L:8L, ...){ scale_fill_discrete(..., type = colorblind_pal()(8)[.ColorList]) } # Color scale_color_colorblind7 = function(.ColorList = 2L:8L, ...){ scale_color_discrete(..., type = colorblind_pal()(8)[.ColorList]) }<\/code><\/pre> The default value of .ColorList<\/code> is 2:8<\/code> because there are 8 colors in the colorblind palette, and the first is black (the one we want to drop).
.ColorList<\/code>的默认值为2:8<\/code> ,因为色盲调色板中有 8 种颜色,第一种是黑色(我们要丢弃的颜色)。
Usage用法<\/h1>Use the functions like any other scale<\/code> function:
像使用任何其他scale<\/code>函数一样使用函数:
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = "dodge") + scale_fill_colorblind7()<\/code><\/pre>
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = "dodge") + scale_fill_colorblind7(breaks = diamonds$cut)<\/code><\/pre>
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = "dodge") + scale_fill_colorblind7(.ColorList = c(3,4,7,6,1))<\/code><\/pre>
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.