0

I have a (probably) easy problem with histograms in ggplot2 when using facet_wrap with this sample-table

res<-data.frame(c('a1','a1','a1','a1','a2','a2'),c('b1','b1','b2','b3','b1','b2'),c(1.0,1.5,2.5,4.0,1.0,2.5))
colnames(res)<-c("A","B","C")

I would have expected

ggplot(res,aes(res$C, fill=res$A))+geom_histogram(binwidth=1)+facet_wrap(~B)

to result (amongst other bars) in one bar in the b3 facet at C=4. However, the only bar at C=4 shows up in the b2 facet. I think by naive idea of facet_wrap doing for ggplots what a group by would do in SQL does not seem to work. Could anyone give me a hint at what I am doing wrong?

Prradep
  • 5,506
  • 5
  • 43
  • 84
  • 5
    don't use the $-operator inside of `aes`, just `ggplot(res, aes(C, fill=A)) + geom_histogram(binwidth=1) + facet_wrap(~B)` – rcs Sep 27 '17 at 11:54
  • Thanks a lot, that helped! The reason for the weird behaviour was that values for different rows were mixed by filtering? Or rather that the association was broken? – Snafusmurf Sep 27 '17 at 12:15
  • 1
    @Snafusmurf `$` breaks the link. You may wish to check out the explanation [here](https://stackoverflow.com/questions/32543340/behavior-ggplot2-aes-in-combination-with-facet-grid-when-passing-variable-wi). – Z.Lin Sep 27 '17 at 13:24
  • thx a lot @Z.Lin – Snafusmurf Sep 27 '17 at 14:26

0 Answers0