分组条形图自定义颜色 [英] Grouped bar graph custom colours

查看:120
本文介绍了分组条形图自定义颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据,并希望创建一个像这样的分组条形图:

I have the following data and wish to create a grouped bar graph like so:

data<-as.data.frame(c("a","b","c","a","b","c"))
colnames(data)<-"Y"

data$X<-c("x","x","x","y","y","y")

data$Z<-c(1,2,3,1,2,3)

ggplot(data, aes(x=X, y=Z, fill=Y) +
  geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
  scale_fill_manual(values=c("red","red","red","blue","blue","blue"))

在代码的最后一行中,我希望更改颜色条-我希望组 x的所有条都被染成红色,组 y的条被染成蓝色。但是,如以下结果所示,我无法使用 scale_fill_manual

In the last line of the code I wish to change the colours of the bars - I would like that all of the bars of group "x" be coloured red and the bars of group "y" to be coloured blue. However as the result below shows, I cannot manage to do this using scale_fill_manual.

推荐答案

您需要获取填充 m设置到右侧变量上:

You need to get group and fill mapped to the right variable:

ggplot(data, aes(x=X, y=Z, group=Y, fill=X)) +
         geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) + 
  scale_fill_manual(values=c("red","blue"))

这篇关于分组条形图自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆