Can't add a probability-curve on the histogram

倖福魔咒の 提交于 2019-12-03 17:14:47

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)), ...)
                              })
)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!