Friday, April 5, 2013

RG#11:multiple histograms with normal distribution or density curve overlayed

#data
set.seed(1233)

data1 < - data.frame(pop =c(rep("A x B", 200), rep("A x C", 200), rep("B x C", 200) ) , var1 = c(rnorm(1000, 90,10), rnorm(1000, 50, 10), rnorm(1000, 20, 30)))

#plot
require(lattice)


# plot overall distribution
histogram(~ var1, data= data1, nint = 12, xlab = "trait1(measuring unit)", type = "density", panel = function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath = dnorm, col = "black",
args = list(mean=mean(x),sd=sd(x)))
} )




# plot by each group
histogram(~ var1|factor(pop), data= data1, nint = 10, xlab = "trait1(measuring unit)", type = "density",
panel = function(x, ...) {
panel.histogram(x, col = "darkgreen", ...)
panel.mathdensity(dmath = dnorm, col = "red",
args = list(mean=mean(x),sd=sd(x)))
} )



 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.