用R创建一个3D直方图。

时间:2021-01-20 14:54:33

How can I create a 3D histogram with R?

如何用R创建三维直方图?

For example, I have two variables to be counted for the number of times they fall in a defined two dimensional 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轴是两个变量的计数。

3 个解决方案

#1


7  

have a look at package hexbin to calculate and display, or e.g. ggplot's stat_bin2d / stat_binhex for display. You get 2 spatial coordinates which is all your screen or paper can do plus a 3rd, colour-coded dimension.

查看包hexbin的计算和显示,或者,例如ggplot的stat_bin2d / stat_binhex来显示。你得到2个空间坐标,这是你的屏幕或纸张可以做的,加上第三个,颜色编码的维度。

Note that How does one plot a 3D stacked histogram in R? is quite a duplicate of this question (but the 3rd dimension was discussed spatially there).

请注意,如何在R中绘制3D叠加直方图?这是这个问题的一个重复(但第三个维度是在空间上讨论的)。

#2


1  

The rgl package has a function hist3d (not actually in the doc, but you can call it and also see the code).

rgl包有一个函数hist3d(实际上不是在文档中,但是可以调用它,也可以看到代码)。

Though this hist3d is, for me displaying a 2-dimensional histograms (input = x,y) in 3 dimensions.

虽然这个hist3d是,我在三维空间中显示一个二维直方图(输入= 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是每个维度中的箱子数)。

#3


1  

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)

#1


7  

have a look at package hexbin to calculate and display, or e.g. ggplot's stat_bin2d / stat_binhex for display. You get 2 spatial coordinates which is all your screen or paper can do plus a 3rd, colour-coded dimension.

查看包hexbin的计算和显示,或者,例如ggplot的stat_bin2d / stat_binhex来显示。你得到2个空间坐标,这是你的屏幕或纸张可以做的,加上第三个,颜色编码的维度。

Note that How does one plot a 3D stacked histogram in R? is quite a duplicate of this question (but the 3rd dimension was discussed spatially there).

请注意,如何在R中绘制3D叠加直方图?这是这个问题的一个重复(但第三个维度是在空间上讨论的)。

#2


1  

The rgl package has a function hist3d (not actually in the doc, but you can call it and also see the code).

rgl包有一个函数hist3d(实际上不是在文档中,但是可以调用它,也可以看到代码)。

Though this hist3d is, for me displaying a 2-dimensional histograms (input = x,y) in 3 dimensions.

虽然这个hist3d是,我在三维空间中显示一个二维直方图(输入= 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是每个维度中的箱子数)。

#3


1  

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)