问题
I am having trouble trying to set my errorbars to manual colors to follow the color scheme I have set for my points. Essentially, I would like the color of each errorbar to match the fill color of it's associated point.
Create dataframe
mean<-c(4,5,6,7)
CI<-c(0.5,0.4,0.3,0.2)
stress<-c(1,2,3,4)
a<-c(10,10,20,20)
b<-c(7.5,7.5,8,8)
data<-data.frame(mean,CI,stress,a,b)
Original Plot
library(ggplot2)
p<- ggplot(data, aes(a, mean))
p+geom_point()+
geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
geom_point(aes(fill=factor(stress)),size=8, shape=21)+
scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
scale_x_continuous("Level A",breaks=c(10,20))+
ylab(expression("Level B"))+
theme_bw(17)
Attempt to create manually colored error bars, but didn't work
p<- ggplot(data, aes(a, mean))
p+geom_point()+
geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
scale_color_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
geom_point(aes(fill=factor(stress)),size=8, shape=21)+
scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
scale_x_continuous("Level A",breaks=c(10,20))+
ylab(expression("Level B"))+
theme_bw(17)
p<- ggplot(data, aes(a, mean))
p+geom_point()+
geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI), width=0.3, color=factor(stress))+
scale_fill_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
geom_point(aes(fill=factor(stress)),size=8, shape=21)+
scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
scale_x_continuous("Level A",breaks=c(10,20))+
ylab(expression("Level B"))+
theme_bw(17)
回答1:
Works for me.
ggplot(data, aes(a, mean)) +
geom_point()+
geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI, color=factor(stress)), width=0.3)+
scale_color_manual("Stress", breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
geom_point(aes(fill=factor(stress)),size=8, shape=21)+
scale_fill_manual("Stress",breaks=c(1,2,3,4),values=c("#0072B2", "#009E73", "#E69F00", "#D55E00"))+
scale_x_continuous("Level A",breaks=c(10,20))+
ylab(expression("Level B"))+
theme_bw(17)
来源:https://stackoverflow.com/questions/34082149/color-by-manual-scale-for-geom-errorbar