晶格:叠加密度图,其中包含直方图中的参数

时间:2014-06-10 20:43:31

标签: r lattice

以下四个调用返回的内容似乎完全相同。如何控制面板密度图?谢谢。

 library(lattice)
 df <- data.frame( y = runif(100) , p = rep(c('a','b'),50) )

 histogram(~ y | p , data = df , 
      type = "density",
      panel=function(x, ...) {
        panel.histogram(x, ...)
        panel.densityplot(x, ...)
      })


 histogram(~ y | p , data = df , 
      type = "density",
      panel=function(x, ...) {
        panel.histogram(x, ...)
        panel.densityplot(x, bw=100,kernel="gaussian",...)
      })

 histogram(~ y | p , data = df , 
      type = "density",
      panel=function(x, ...) {
        panel.histogram(x, ...)
        panel.densityplot(x, dargs=list(bw=100,kernel="gaussian"),...)
      })

 histogram(~ y | p , data = df , 
      type = "density", bw=100,kernel="gaussian" ,
      panel=function(x, ...) {
        panel.histogram(x, ...)
        panel.densityplot(x, ...)
      })

1 个答案:

答案 0 :(得分:3)

正如上面的评论所述,您对histogram()的第三次电话非常接近。您只需要写darg而不是dargs

以下示例显示darg确实如?panel.densityplot中所述,可以控制平滑参数:

library(gridExtra)  ## For grid.arrange()
library(lattice)
df <- data.frame(y = runif(100) , p = rep(c('a','b'),50))

p1 <- histogram(~ y | p , data = df , 
          type = "density",
          panel=function(x, ...) {
             panel.histogram(x, ...)
             panel.densityplot(x, darg=list(bw = 1, kernel="gaussian"),...)
      })

p2 <- histogram(~ y | p , data = df , 
          type = "density",
          panel=function(x, ...) {
              panel.histogram(x, ...)
              panel.densityplot(x, darg=list(bw = 0.2, kernel="gaussian"),...)
      })

grid.arrange(p1,p2)

enter image description here