简体   繁体   English

使用R创建3D直方图

[英]Creating a 3D histogram with R

How can I create a 3D histogram with R? 如何用R创建3D直方图?

For example, I have two variables to be counted for the number of times they fall in a defined two dimensional bin. 例如,我有两个变量要计算它们落入定义的二维bin的次数。 So I have two variables in the X and Y axis, while the Z axis is the count of the two variables. 所以我在X和Y轴上有两个变量,而Z轴是两个变量的计数。

have a look at package hexbin to calculate and display, or eg ggplot's stat_bin2d / stat_binhex for display. 查看包hexbin来计算和显示,或者例如ggplot的stat_bin2d / stat_binhex用于显示。 You get 2 spatial coordinates which is all your screen or paper can do plus a 3rd, colour-coded dimension. 你得到2个空间坐标,这是你的所有屏幕或纸张可以做的加上第三个颜色编码的尺寸。

Note that How does one plot a 3D stacked histogram in R? 请注意, 如何在R中绘制3D堆叠直方图? is quite a duplicate of this question (but the 3rd dimension was discussed spatially there). 与这个问题完全相同(但第三维在那里进行了空间讨论)。

The rgl package has a function hist3d (not actually in the doc, but you can call it and also see the code). rgl包有一个函数hist3d(实际上不在doc中,但你可以调用它,也可以看到代码)。

Though this hist3d is, for me displaying a 2-dimensional histograms (input = x,y) in 3 dimensions. 虽然这个hist3d是,我为3维展示了二维直方图(input = x,y)。

If that is what you want here is the code (from rgl): 如果这就是你想要的代码(来自rgl):

> hist3d
function(x,y=NULL,nclass="auto",alpha=1,col="#ff0000",scale=10)
  {
  save <- par3d(skipRedraw=TRUE)
  on.exit(par3d(save))
  xy <- xy.coords(x,y)
  x <- xy$x
  y <- xy$y
  n<-length(x)
  if (nclass == "auto") { nclass<-ceiling(sqrt(nclass.Sturges(x))) }
  breaks.x <- seq(min(x),max(x),length=(nclass+1))
  breaks.y <- seq(min(y),max(y),length=(nclass+1))
  z<-matrix(0,(nclass),(nclass))
  for (i in 1:nclass) 
    {
    for (j in 1:nclass) 
      {
      z[i, j] <- (1/n)*sum(x < breaks.x[i+1] & y < breaks.y[j+1] & 
                            x >= breaks.x[i] & y >= breaks.y[j])
      binplot.3d(c(breaks.x[i],breaks.x[i+1]),c(breaks.y[j],breaks.y[j+1]),
                 scale*z[i,j],alpha=alpha,topcol=col)
      }
    }
}

I built my own hist3d to return 3-dimensional histogram (for example to use on Red Green Blue): 我建立了自己的hist3d来返回三维直方图(例如在红绿蓝上使用):

my_hist3d <- function(x,y=NULL,z=NULL, nclass="auto",alpha=1,col="#ff0000",scale=10)
  {

  xyz <- xyz.coords(x,y,z)
  x <- xyz$x
  y <- xyz$y
  z <- xyz$z

  n<-length(x)

  if (nclass == "auto") { nclass<-ceiling(sqrt(nclass.Sturges(x))) }

  breaks.x <- seq(min(x),max(x),length=(nclass+1))
  breaks.y <- seq(min(y),max(y),length=(nclass+1))
  breaks.z <- seq(min(z),max(z),length=(nclass+1))


  h = array(1:(nclass^3), dim=c(nclass,nclass,nclass))

  for (i in 1:nclass) 
    {
    for (j in 1:nclass) 
      {
        for (k in 1:nclass) 
          {
              h[i,j,k] <- (1/n)*sum(x < breaks.x[i+1] & y < breaks.y[j+1] & x >= breaks.x[i] & y >= breaks.y[j] & z < breaks.z[k+1] & z >= breaks.z[k])
          }
      }
    }

  return(h)
}

The variable returned (h) is a three-dimensional matrix of size nclass^3 (nclass is the number of bins in each dimension). 返回的变量(h)是大小为nclass ^ 3的三维矩阵(nclass是每个维度中的bin的数量)。

You can use the next function based on tucson function to plot a histogram in 3d. 您可以使用基于图森函数的下一个函数在3d中绘制直方图。

my_hist3d <- function(x, y, freq=FALSE, nclass="auto") {
  n<-length(x)
  if (nclass == "auto") { nclass<-ceiling(sqrt(nclass.Sturges(x))) }
  breaks.x <- seq(min(x),max(x),length=(nclass+1))
  breaks.y <- seq(min(y),max(y),length=(nclass+1))
  h <- NULL
  for (i in 1:nclass) 
    for (j in 1:nclass) 
      h <- c(h, sum(x <= breaks.x[j+1] & x >= breaks.x[j] & y <= breaks.y[i+1] & y >= breaks.y[i] ) )
  if (freq) h <- h / n
  xx <- as.factor(round(mean(breaks.x[1:2])+(0:(nclass-1))*diff(breaks.x[1:2]), 1))
  yy <- as.factor(round(mean(breaks.y[1:2])+(0:(nclass-1))*diff(breaks.y[1:2]), 1))
  res <- cbind(expand.grid(xx,yy), h)
  colnames(res) <- c(deparse(substitute(x)),deparse(substitute(y)),'Frequency')
  formu <- as.formula(paste("Frequency ~ ", paste(colnames(res)[1:2], collapse= "+")))
  cloud(formu, res, panel.3d.cloud=panel.3dbars, col.facet='lightblue', 
       xbase=1, ybase=1, scales=list(arrows=FALSE, col=1), 
        par.settings = list(axis.line = list(col = "transparent")))
}

library(latticeExtra)

height <- rbeta(2000, 2, 5)
weight <- rgamma(2000, 10)
my_hist3d(height, weight, nclass=10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM