Reputation: 431
I would like to have a colorblind friendly paletter for a barplot in ggplot. I plot the barplot through this code
library(tidyverse)
my_se <- df %>%
group_by(groups) %>%
summarise(n=n(),
sd=sd(mean),
se=sd/sqrt(n))
# Standard error
df %>%
left_join(my_se) %>%
mutate(zone = factor(zone,labels = c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11"))) %>%
ggplot(aes(x=zone, y=mean, fill = groups)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(x=zone, ymin=mean-se, ymax=mean+se), width=0.4, position = position_dodge(.9)) +
ggtitle("using standard error")+scale_fill_discrete(labels = c("GC", "IP", "MR","CS"))+
labs(y= an, x = "Land Cover")+theme_bw()+theme(legend.title =element_blank())+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12))
I have made a custom color palette
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73")
But when I add this snippet code:
scale_fill_manual(values=cbPalette)
to my ggplot code it gives me this message
Scale for 'fill' is already present. Adding another scale for 'fill', which
will replace the existing scale.
Although the colors are changed but my whole legend labels get changed with it. I want to keep my legend variables as "GC", "IP", "MR","CS".
How can I change the colors of the bars with not effecting the legend labels of variables?
Upvotes: 1
Views: 497
Reputation: 39595
Pointing out to great suggestion from @Dave2e you can try this (Data used from similar questions you posted):
df %>%
left_join(my_se) %>%
mutate(zone = factor(zone,labels = c("x1","x2","x3","x4","x5","x6","x7","x8","x9","x10","x11"))) %>%
ggplot(aes(x=zone, y=meangpp, fill = groups)) +
geom_col(position = position_dodge()) +
geom_errorbar(aes(x=zone, ymin=meangpp-se, ymax=meangpp+se), width=0.4, position = position_dodge(.9)) +
ggtitle("using standard error")+
scale_fill_manual(labels = c("GC", "IP", "MR","CS"),values=cbPalette)+
labs(y= 'an', x = "Land Cover")+theme_bw()+theme(legend.title =element_blank())+
theme(legend.text=element_text(size=11),axis.text.y=element_text(size=11.5),
axis.text.x=element_text(size=11.5),axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12))
Upvotes: 2