不能在直方图上加上概率曲线吗

时间:2021-11-11 14:56:43

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!

我很高兴得到任何建议!

2 个解决方案

#1


3  

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:

在直方图函数中使用上述mydata2对象不会产生X1的密度图,因为均值和sd返回NA。添加na。rm =对于这两个函数都将返回面板的值。mathdensity可以使用:

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

不能在直方图上加上概率曲线吗

#2


0  

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

不能在直方图上加上概率曲线吗

#1


3  

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:

在直方图函数中使用上述mydata2对象不会产生X1的密度图,因为均值和sd返回NA。添加na。rm =对于这两个函数都将返回面板的值。mathdensity可以使用:

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

不能在直方图上加上概率曲线吗

#2


0  

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

不能在直方图上加上概率曲线吗