R ggplot2改变了躲避条形图的颜色

时间:2012-10-01 13:30:35

标签: r ggplot2 scale

我正在处理以下数据的ggplot2图:

means <- c(2.4,3,3,3.16,2.5,2.5,3,4.5)
sds <- c(1.0,1.2,1.0,1.1,2.1,0.7,2.8,0.7)
teams <- c(1,1,1,1,2,2,2,2)
scales <- c(1,2,3,4,1,2,3,4)

datas <- data.frame(teams, scales, means, sds)

感谢非常积极的助手,情节如下:

graph <- 
    ggplot(data=datas,  aes(scales, y=means, group=teams)) + 
    geom_bar(aes(fill=teams), stat="identity", 
             position="dodge") + 
    geom_errorbar(aes(ymin= means - sds, ymax = means + sds, width=0.2), 
                  position=position_dodge(width=0.90)) +
    coord_flip()

现在我想将条形颜色更改为特殊颜色。 我得到了一个提示,scale_fill_manual会这样做(我尝试了scale_fill_manual(values = c('#0023a0','#f9a635'))+ ...)但我得到的只是一个错误(连续值提供给离散比例)。有什么想法吗?

1 个答案:

答案 0 :(得分:8)

ggplot(data = datas,  aes(scales, y = means, group = teams)) + 
    geom_bar(aes(fill = as.factor(teams)), stat = "identity", 
             position = "dodge") + 
    geom_errorbar(aes(ymin = means - sds, ymax = means + sds, width=0.2), 
                  position = position_dodge(width = 0.90)) +
    coord_flip()  + scale_fill_manual("Teams",values = c('#0023a0', '#f9a635'))