我在尝试将错误栏设置为手动颜色以遵循我为我的点设置的配色方案时遇到了麻烦。本质上,我希望每个误差条的颜色与其关联点的填充颜色相匹配。
创建数据框
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)
原始情节
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)
尝试手动创建彩色误差线,但没有成功
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)