简体   繁体   中英

geom_bar with multiple fill colour in ggplot2

I have this kind of dataset :

comb<-data.frame(nom=c("A","B","C","A","B","C"),type=c(rep("1",3),rep("2",3)),val=c(1,3,2,3,2,2))

I Would like to have this result with ggplot2 :

在此处输入图片说明

But I just have this

ggplot()+    geom_bar(data=comb,aes(x=nom, y=val,fill=type),stat='identity',position='dodge')

在此处输入图片说明

Do you have any solution for me?

If you want all bars to have a different color, you have to use the interaction of type and nom :

library(ggplot2)
ggplot() +  
  geom_bar(data = comb,aes(x = nom, y = val, fill = interaction(type, nom)),
           stat = 'identity', position = 'dodge')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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