【问题标题】:add different colour to bar plot为条形图添加不同的颜色
【发布时间】:2014-08-01 15:35:02
【问题描述】:

我使用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)

我想知道如何将此标准错误添加到相应批次中

如何将其添加到栏?

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    请提供更容易回答的数据。在这里,我创建了一个新集合。

    1. 我已经包含了马丁对颜色的回答
    2. 如果每条柱有一个 se 值,则应直接将其添加到数据框中并使用 geom_errorbar()。查看文档了解更多信息。

    .

    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)
    

    【讨论】:

      【解决方案2】:

      第一个问题:使用scale_color_manualscale_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
      

      第二个问题:向hereR: ggplot2 barplot and error bar 寻求帮助。

      【讨论】:

        猜你喜欢
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 2017-02-18
        相关资源
        最近更新 更多