Paul Lemmens
Paul Lemmens

Reputation: 635

Different histograms when plotted facetted or unfacetted (ie, overlaid) with ggplot2

I am running a bootstrap procedure to get a better feeling for some of the statistics that I'm getting out of a mixed model based on relatively small number of participants. For two conditions I'm plotting the histogram of the bootstrapped means (link to data).

The thing is that when I plot the histograms on top of each other (one facet/panel) then I'm seeing a clear bimodal histogram for the B treatment but this bimodal peak disappears when plotting using a facet for each treatment.

p <- ggplot(data=bstr3, mapping=aes(x=m, fill=treatment)) +
  geom_histogram(binwidth=1, alpha=0.4) + 
  scale_fill_manual(values=c('A'='red', 'B'='blue')) 
p + coord_cartesian(xlim=c(-60, 0))

enter image description here

However, when I'm plotting the treatment as facets, the bimodal peaks disappear.

p + facet_wrap(~treatment, ncol=1) + coord_cartesian(xlim=c(-60, 0))

faceted plot

Based on the ggplot book and internet I fail to understand whether/why this is intended or potentially a bug. I did find this question that alludes to a bug, but the post is a year old and I have the latest updates to R and ggplot2.

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base     

other attached packages:
[1] plyr_1.8             reshape2_1.2.2       knitrBootstrap_0.6.5
[4] markdown_0.6.1       knitr_1.3            ggplot2_0.9.3.1     

loaded via a namespace (and not attached):
 [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       evaluate_0.4.4    
 [5] formatR_0.8        grid_3.0.1         gtable_0.1.2       labeling_0.2      
 [9] MASS_7.3-27        munsell_0.4.2      proto_0.3-10       RColorBrewer_1.0-5
[13] scales_0.2.3       stringr_0.6.2      tools_3.0.1       

Upvotes: 3

Views: 460

Answers (1)

Roland
Roland

Reputation: 132969

The default position adjustment used by geom_histogram is position_stack (which seems a strange default for histograms). You want position_identity.

set.seed(42)
DF <- data.frame(x=rnorm(1000,5,0.5),y=rnorm(1000,7,1))
library(reshape2)
DF <- melt(DF)

library(ggplot2)
p <- ggplot(data=DF, mapping=aes(x=value, fill=variable)) +
  geom_histogram(binwidth=1, alpha=0.4) + 
  scale_fill_manual(values=c('x'='red', 'y'='blue')) 
print(p)

enter image description here

p <- ggplot(data=DF, mapping=aes(x=value, fill=variable)) +
  geom_histogram(binwidth=1, alpha=0.4, position="identity") + 
  scale_fill_manual(values=c('x'='red', 'y'='blue')) 
print(p)

enter image description here

Upvotes: 11

Related Questions