【问题标题】:Creating a 3D histogram with R使用 R 创建 3D 直方图
【发布时间】:2011-12-02 19:31:47
【问题描述】:

如何使用 R 创建 3D 直方图?

例如,我有两个变量要计算它们落在定义的二维 bin 中的次数。所以我在 X 和 Y 轴上有两个变量,而 Z 轴是这两个变量的计数。

【问题讨论】:

  • 这个问题更适合 CrossValidated。我已将其标记为版主注意。
  • 我不得不承认我不明白结束语。恕我直言,这个问题可以很容易地回答与软件相关的问题(我理解这是这里的主题):例如查看软件包 hexbin 或 ggplot 的 stat_bin2d / stat_binhex。您将获得 2 个空间坐标,这是您的屏幕或纸张可以做的所有事情,再加上第 3 个颜色编码的维度。话虽如此,它可能更值得作为stackoverflow.com/questions/2405575/… 的副本关闭(然而,这 3 个维度是纯粹在空间上讨论的)。
  • 顺便说一句:我将两个变量 X 和 Y 以及它们在 Z 中的计数称为二维直方图。
  • @cbeleites:是的,这个问题已经重新打开(见修订)。如果这是重复的,那么我们应该这样关闭它。如果其他问题缺少详细信息,请随时在此处添加答案。
  • 您是否考虑过使用热图? pheatmap 包可以很好地制作它们。

标签: r histogram


【解决方案1】:

查看软件包 hexbin 以计算和显示,或者例如ggplot 的 stat_bin2d / stat_binhex 用于显示。您将获得 2 个空间坐标,这是您的屏幕或纸张可以做的所有事情,再加上第 3 个颜色编码的维度。

请注意,How does one plot a 3D stacked histogram in R? 与此问题完全重复(但在空间上讨论了第 3 维)。

【讨论】:

  • 只是好奇:线程是重新打开还是我弄错了?
【解决方案2】:

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

虽然这个 hist3d 是,但对我来说,在 3 维中显示 2 维直方图(输入 = x,y)。

如果这是你想要的代码(来自 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)
      }
    }
}

我构建了自己的 hist3d 以返回 3 维直方图(例如用于红绿蓝):

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

返回的变量 (h) 是一个大小为 nclass^3 的三维矩阵(nclass 是每个维度的 bin 数)。

【讨论】:

    【解决方案3】:

    您可以使用基于 tucson 函数的 next 函数来绘制 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)
    

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多