繁体   English   中英

R ggplot 条形图:条形图 colors 不会改变

[英]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")

我想要某些 colors 的条,但是:

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

plot 已创建,但默认为 colors,而不是指定的那些。 任何人都明白为什么?

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

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

Plot:

在此处输入图像描述

样本数据:

 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)

暂无
暂无

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

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