简体   繁体   English

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

[英]R ggplot2 change colours of dodged barchart

I working on a ggplot2 plot of the following data: 我正在研究以下数据的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)

Thanks to a very active helper, the plot looks like this: 感谢非常积极的帮助,情节看起来像这样:

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()

Now I want to change the colour of the bars into special colours. 现在我想将条形颜色更改为特殊颜色。 I got a hint that scale_fill_manual would do it (I tried scale_fill_manual(values= c('#0023a0', '#f9a635'))+...) but all I get is an error (Continuous value supplied to discrete scale). 我得到了一个提示,scale_fill_manual会这样做(我尝试了scale_fill_manual(values = c('#0023a0','#f9a635'))+ ...)但我得到的只是一个错误(连续值提供给离散比例)。 Any ideas? 有任何想法吗?

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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM