Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top