简体   繁体   中英

manually editing color of confidence intervals and mean across ggplot2 facets

I can choose the color of my confidence intervals in ggplot, but it fails when I try to plot these intervals in different facets.

This works as expected

df=data.frame(Effectsize=seq(from=1, to=20,   by=1),percentile=as.factor(rep(c("10th","50th"), 10)),   name=as.factor(rep(c("Group1","Group2","Group2","Group1","Group1"), 4)))


ggplot(df, aes(y=Effectsize, x=percentile))+xlab("")+ylab("Effect size (Slope/SD)")+
geom_boxplot(fill="transparent", colour="transparent")+
stat_summary(fun = mean, geom = "point", colour=c("turquoise", "orange"))+
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", colour=c("turquoise", "orange"))

在此处输入图像描述

But when i try to plot that in facets, it doesn't plot the two ribbons with their colors per facet.

ggplot(df, aes(y=Effectsize, x=percentile))+xlab("")+ylab("Effect size (Slope/SD)")+
geom_boxplot(fill="transparent", colour="transparent")+
stat_summary(fun = mean, geom = "point", colour=c("turquoise", "orange"))+
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", colour=c("turquoise", "orange"))+
facet_grid(. ~ name)+theme(legend.position = "none") 

and comes the error: Error: Aesthetics must be either length 1 or the same as the data (4): colour Run rlang::last_error() to see where the error occurred.

Adding

 scale_fill_manual(values=c("turquoise", "orange")) + 

did not help either.

What could be the work around?

Just change:

colour=c("turquoise", "orange")

to

colour=c("turquoise", "orange", "turquoise", "orange")

ggplot(df, aes(y=Effectsize, x=percentile))+xlab("")+ylab("Effect size (Slope/SD)")+
  geom_boxplot(fill="transparent", colour="transparent")+
  stat_summary(fun = mean, geom = "point", colour=c("turquoise", "orange", "turquoise", "orange"))+
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", colour=c("turquoise", "orange", "turquoise", "orange"))+
  facet_grid(. ~ name)+
  theme(legend.position = "none") 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM