R ggplot条形图:条形颜色不会改变

R ggplot bar chart: bars colors won't change

比如说,下面的代码:

x_axe<-c(1,2,3)
y_axe_a<-c(10,20,30)
y_axe_b<-c(100,200,300)

for_barchart<-data.frame(x_axe, y_axe_a, y_axe_b)

chart<-ggplot(data=for_barchart, aes(x=x_axe, y=y_axe_a, color=x_axe)) +
  geom_bar(stat="identity", fill = x_axe, show.legend = FALSE, width = 0.75)+
  ylim(0,45)+
  ylab("so and so")

我想要某些颜色的条形图但是:

chart + scale_fill_manual(values=c("yellow", "red", "blue"))

绘图已创建,但使用的是默认颜色,而不是指定的颜色。有人知道为什么吗?

为列中的不同颜色分配顺序是一件简单的事情。

for_barchart<-data.frame(x_axe, y_axe_a, y_axe_b)

for_barchart$colors=c("yellow", "red", "blue")
for_barchart$colors<-factor(for_barchart$colors, levels=c("yellow", "red", "blue"))

library(ggplot2)

(chart<-ggplot(data=for_barchart, aes(x=x_axe, y=y_axe_a)) +
        geom_bar( aes(fill = colors),stat="identity", show.legend = FALSE, width = 0.75)+
        scale_fill_manual(values=c("yellow", "red", "blue"))+
        ylim(0,45)+
        xlab("Groups")+
        ylab("so and so"))+
        theme_minimal()

剧情:

示例数据:

 x_axe<-c(1,2,3)
 y_axe_a<-c(10,20,30)
 y_axe_b<-c(100,200,300)

其他解决方案是,如果 x_axe<-c(1,2,3) 被定义为字符 x_axe<-c("1","2","3")

示例代码:

(chart<-ggplot(data=for_barchart, aes(x=x_axe, y=y_axe_a, fill=x_axe)) +
  geom_bar(stat="identity", show.legend = FALSE, width = 0.75)+
  scale_fill_manual(values=c("1"="yellow", "2"="red", "3"="blue"))+
  ylim(0,45)+
  xlab("Groups")+
  ylab("so and so"))+
  theme_minimal()

示例数据:

 x_axe<-c("1","2","3")
 y_axe_a<-c(10,20,30)
 y_axe_b<-c(100,200,300)