I'm trying do display multiple histograms with one plot with the lattice-package.
That's my code so far:
histogram(~ X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10, data=mydata,
type = "density",layout=c(5,2),
panel=function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath=dnorm, col="black",
args=list(mean=mean(x), sd=sd(x)), ...)
})
The problem is, that it won't plot the probability-curve. It doesn't give me an error back, so the code looks good, I think.
I also tried it with only one variable and it didn't work either:
histogram(~ X1, data=mydata,
type = "density",layout=c(5,2),
panel=function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath=dnorm, col="black",
args=list(mean=mean(x), sd=sd(x)), ...)
})
Does anyone see an error in my code? Or could be something wrong in my data?
I'm glad for any advice!
Could it be that your data contain missing values?
# Create example data (no missings)
mydata <- data.frame(X1 = rpois(1000, 12), X2 = rnorm(1000, 12, sqrt(12)))
# Create some missing (NA) entries
mydata2 <- mydata
mydata2[sample(seq_len(nrow(mydata2)), 10), 1] <- NA
Using the above mydata2 object in the histogram function produces no density plot for X1, since mean and sd return NA. Adding na.rm = TRUE to both those functions will return values that panel.mathdensity can use:
histogram(~ X1 + X2, data=mydata2,
type = "density",layout=c(1,2),
panel=function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath=dnorm, col="black",
# Add na.rm = TRUE to mean() and sd()
args=list(mean=mean(x, na.rm = TRUE),
sd=sd(x, na.rm = TRUE)), ...)
})

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With