我使用以下代码使用ggplot
构建了以下图:
ggplot(data, aes(x=Variable, y=Value, fill=Yield.Type)) +
geom_bar(stat="identity", position="dodge")
我有两个问题:
1)如何更改条形图的颜色:我想将粉红色条纹颜色为白色和蓝色条形图,为灰色,带黑色边框。如果在代码中,我使用col="White",fill="White"
,它会使用相同的颜色为它们着色并将它们叠加在一起
2)对于每个条形,我在单独的向量中有标准错误
For pink bars, se1<-c(0.08,0.07,0.08,0.07)
For blue bars, se2<-c(0.07,0.1,0.06,0.06)
我想知道如何将此标准错误添加到resepctive批处理
如何将其添加到栏中?
答案 0 :(得分:0)
第一个问题:使用scale_color_manual
和scale_fill_manual
(请参阅add different colour to bar plot)
p <- ggplot(...)
p + scale_color_manual(values=c("white","black")) +
scale_fill_manual(values=c("white", "grey"))
p
第二个问题:查看here或R: ggplot2 barplot and error bar寻求帮助。
答案 1 :(得分:0)
请提供更容易回答的数据。在这里,我创建了一个新的集合。
Variable <- factor(c("VAr1","VAr1","Var2","Var2","Var3","Var3","VAr4","VAr4"))
Yield.Type <- factor(c('O','R','O','R','O','R','O','R'))
Value <- c(1,2,3,4,3,5,6,5)
se1<-c(0.08,0.07,0.08,0.07,0.1,0.06,0.1,1)
data <- data.frame(Variable,Yield.Type,Value,se1)
limits <- aes(ymax = Value + se1, ymin=Value - se1)
dodge <- position_dodge(width=0.8)
ggplot(data, aes(x=Variable, y=Value,fill=Yield.Type,colour=Yield.Type)) +
geom_bar(stat="identity", position="dodge")+
scale_color_manual(values=c("black","black")) +
scale_fill_manual(values=c("white", "grey"))+
geom_errorbar(limits, position=dodge,width=0.1)