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:

enter image description here

But I just have this

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

enter image description here

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

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top