【发布时间】:2015-12-04 06:23:36
【问题描述】:
我在尝试将错误栏设置为手动颜色以遵循我为我的点设置的配色方案时遇到了麻烦。本质上,我希望每个误差条的颜色与其关联点的填充颜色相匹配。
创建数据框
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)
【问题讨论】:
-
我在您第一次尝试手动将颜色设置为
geom_errorbar(aes(ymax=mean+CI,ymin=mean-CI, color=factor(stress)), width=0.3)时修改了geom_errorbar。手柄的颜色与填充相同。这就是你所追求的吗? -
我试图让手柄的颜色与填充颜色相同,是的。但是,我仍然没有在手柄上获得正确的颜色。它们是彩色的,但与点的填充不匹配。