Alessandro Jacopson
Alessandro Jacopson

Reputation: 18603

How to plot, in the same graph, the histogram and the frequency polygon of two sets of data with ggplot2 in R

I have two sets of data and I would like to get a single graph with the histogram and frequency polygon for each set of data.

My data frame df is like this one:

'data.frame':   20000 obs. of  2 variables:
 $ measure   : num  -0.566 0.321 0.125 1.353 -1.288 ...
 $ processing: Factor w/ 2 levels "before","after": 1 1 1 1 1 1 1 1 1 1 ...

     measure processing
1 -0.5656801     before
2  0.3210458     before
3  0.1252706     before
4  1.3532248     before
5 -1.2877305     before
6  0.3225545     before

My code is the following:

png("figure_%d.png")
set.seed(2014)
n <- 10000
before <- rnorm(n)
df_1 <- data.frame(measure=before)
df_1$processing <- factor("before")

after <- before-rnorm(n,mean=1,sd=0.1)
df_2 <- data.frame(measure=after)
df_2$processing <- factor("after")

df<-rbind(df_1,df_2)

library(ggplot2)


print(ggplot(df, aes(measure,colour=processing))+geom_freqpoly())

print(ggplot(df, aes(measure,fill=processing))+geom_density(alpha=0.5))

print(ggplot(df_1, aes(measure,fill=processing))+geom_histogram(alpha=0.5))
print(ggplot(df_2, aes(measure,fill=processing))+geom_histogram(alpha=0.5))
print(ggplot(df, aes(measure,fill=processing))+geom_histogram(alpha=0.5))

print(ggplot(df, aes(measure,fill=processing,colour=processing))+geom_freqpoly()+geom_histogram(alpha=0.5))

Now, after

ggplot(df, aes(measure,colour=processing))+geom_freqpoly()

I get the following figure

enter image description here

where the two frequency polygon are as expected.

After

ggplot(df, aes(measure,fill=processing))+geom_density(alpha=0.5)

I get the following figure

enter image description here

and where the two densities overlap I get the expected "blended" color.

Now I would like to get a figure with the two histograms; first of all I draw the two histograms in two separate figures: with the code

ggplot(df_1, aes(measure,fill=processing))+geom_histogram(alpha=0.5)

I get the following figure

enter image description here

and with the code

ggplot(df_2, aes(measure,fill=processing))+geom_histogram(alpha=0.5)

I get the following figure

enter image description here

both the two histograms are as expected.

The problem starts when I try to plot both the histogram in the same graph, with this code

ggplot(df, aes(measure,fill=processing))+geom_histogram(alpha=0.5)

I get this figure

enter image description here

and I can't explain why the green histogram is higher than the red one. Furthermore, where the two histograms "overlap", I do not get a "blended" color.

Trying to add the frequency polygon worsens the problem, with this code

ggplot(df, aes(measure,fill=processing,colour=processing))+geom_freqpoly()+geom_histogram(alpha=0.5)

I get this figure

enter image description here

where the frequency polygons seems to me correct but the histograms are wrong like in the previous figure.

What am I doing wrong?

The output from version is

platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          0.2                         
year           2013                        
month          09                          
day            25                          
svn rev        63987                       
language       R                           
version.string R version 3.0.2 (2013-09-25)
nickname       Frisbee Sailing 

The output from sessionInfo() is

R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       grid_3.0.2        
 [5] gtable_0.1.2       labeling_0.2       MASS_7.3-29        munsell_0.4.2     
 [9] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2    
[13] scales_0.2.3       stringr_0.6.2 

Upvotes: 1

Views: 5167

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

Use geom_histogram with the argument position = "identity". The default value for position is "stack". In this case, the bars do not overlap but are stacked.

geom_histogram(alpha = 0.5, position = "identity")

The complete code:

library(ggplot2)
ggplot(df, aes(measure, fill = processing)) + 
  geom_histogram(alpha = 0.5, position = "identity")

enter image description here

Upvotes: 5

Related Questions