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)), ...)
})
Without your data it is hard to help you.
This is a simple example, maybe can help you. I try to keep your settings and correct some ones.
library(lattice)
dat <- data.frame(X1 = rnorm(10000),Y1 =rnorm(10000))
histogram(~X1+Y1,
data = dat,
main=list(
label="Main plot title",
cex=1.5),
xlab=list(
label="Custom x-axis label",
cex=0.75),
ylab=list(
label="Your Y label ",
cex=1.2),
scales=list(cex=0.5),
layout = c(1,2),
par.settings = list(
type = "density",
panel=function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath=dnorm, col="black",
args=list(mean=mean(x), sd=sd(x)), ...)
})
)