忽略RColorBrewer调色板上的更明亮的颜色以在ggplot2中使用 [英] Omit Brighter colors on RColorBrewer palette to use in ggplot2
问题描述
我想在RColorBrewer的橙色"调色板中使用较深的颜色,以便在我的ggplot(条形图)中使用.但是,我无法做到.帮助.下面是示例代码:
I'd like to use the darker colors in the "Oranges" palette in RColorBrewer to use in my ggplot (bargraph). I however am unable to. Help. Below is the sample code:
my_palette = brewer.pal(n = 9, "Oranges")[4:9]
### Bar Plots ###
ggplot(Sample_df, aes(x = Sample_Score), fill = Sample_Score) +
geom_bar(stat = "count", width = 0.8) + scale_fill_brewer(palette= my_palette) + scale_y_log10(labels = comma) +
labs(title="Distribution of customers according to Sample Score",x="Sample Score", y="Number of Customers") +
theme(plot.title = element_text(face = "bold", size = 10),
axis.title.x = element_text(face = "bold", size = 8),
axis.title.y = element_text(face = "bold", size = 8), legend.position="none")
推荐答案
这是因为您正在使用 scale_fill_brewer()
.该命令需要一个 RColorBrewer
调色板名称.(例如橙色"
).它无法读取您的自定义调色板名称 my_palette
,而是使用 scale_fill_manual()
This is because you are using scale_fill_brewer()
. This command expects an RColorBrewer
palette name. (e.g. "Oranges"
). It cannot read your custom palette name my_palette
Instead, use scale_fill_manual()
我创建了一个自己的可复制示例:
I created an own reproducible example:
library(RColorBrewer)
library(ggplot)
set.seed(1234)
df <- data.frame(x=runif(10,0,1), y=runif(10,0,1), c=factor(sample(1:5,10,replace=T)))
ggplot(data=df, aes(x=x,y=y)) + geom_point(aes(color=c)) + scale_color_brewer(palette = 'Oranges')
如您所见,我正在调用 RColorBrewer
调色板"Oranges"
.如您所述,这些颜色非常浅,您可能只想使用较深的颜色.为此,您可以制作一个自定义调色板,并使用 scale_color_manual()
(或您的案例中的 scale_fill_manual()
)代替 scale_color_brewer()代码>.
As you can see, I am calling the RColorBrewer
palette "Oranges"
. As you mentioned, these coloured are very light, and you may want to use only the darker ones. To do this, you can make a custom palette and call it using scale_color_manual()
(or scale_fill_manual()
in your case) instead of scale_color_brewer()
.
my_palette <- brewer.pal(name="Oranges",n=9)[4:9]
ggplot(data=df, aes(x=x,y=y)) + geom_point(aes(color=c)) + scale_color_manual(values = my_palette)
现在将使用您在 my_palette
这篇关于忽略RColorBrewer调色板上的更明亮的颜色以在ggplot2中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!