Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to superimpose distribution curves on histograms using ggplot2 and lattice

Tags:

r

ggplot2

lattice

Say, I am using facet_grid() in ggplot2 to obtain 2 histograms. Now I want to superimpose these histograms with Poisson curves (having different means for the 2 histogram plots/grids) and a second curve of another distribution (for which I want to manually provide the probability function of values). How can this be done?

Constructing an example:

library(ggplot2)

value<-c(rpois(500,1.5))

group<-rep(c("A","B"),250)

data<-data.frame(value,group)

g1<-ggplot(data,aes(value))

g1+geom_histogram(aes(y=..count..),binwidth=1,position="identity")+facet_grid(.~group)

What next?

Alternatively, can it be done using the lattice package?

like image 580
KGM Avatar asked Feb 27 '23 12:02

KGM


2 Answers

The easy way is to plot densities instead of counts and use stat_function()

library(ggplot2)
value<-c(rpois(500,1.5))
group<-rep(c("A","B"),250)
data<-data.frame(value,group)
ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..density..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = dpois, arg = list(lambda = 1.5), colour = "red", fill = NA, n = 9)

If you want counts then you need to convert the densities of dpois to 'counts'

ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..count..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = function(..., total){dpois(...) * total}, arg = list(lambda = 1.5, total = 250), colour = "red", fill = NA, n = 9)
like image 144
Thierry Avatar answered Mar 01 '23 02:03

Thierry


When recently faced with a similar problem (comparing distributions), I wrote up some code for transparent overlapping histograms that might give you some ideas on where to start.

like image 23
chrisamiller Avatar answered Mar 01 '23 00:03

chrisamiller