通过手动缩放为Geom错误栏上色 [英] Color by manual scale for geom errorbar
本文介绍了通过手动缩放为Geom错误栏上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将错误栏设置为手动颜色,以遵循为点设置的配色方案.本质上,我希望每个错误栏的颜色与其关联点的填充颜色相匹配.
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.
创建数据框
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)
尝试创建手动着色的错误栏,但没有成功
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)
推荐答案
为我工作.
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)
这篇关于通过手动缩放为Geom错误栏上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文