无法在直方图上添加概率曲线 [英] Can't add a probability-curve on the histogram

查看:112
本文介绍了无法在直方图上添加概率曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用点阵软件包显示多个直方图和一个图.

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

在直方图函数中使用上面的mydata2对象不会产生X1的密度图,因为meansd返回NA.将na.rm = TRUE添加到这两个函数中将返回panel.mathdensity可以使用的值:

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

这篇关于无法在直方图上添加概率曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆