Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

r - Grouped bar graph custom colours

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

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. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...