Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R创建三维直方图_R_Histogram - Fatal编程技术网

使用R创建三维直方图

使用R创建三维直方图,r,histogram,R,Histogram,如何使用R创建三维直方图 例如,我有两个变量要计算它们落在定义的二维容器中的次数。因此,我在X轴和Y轴上有两个变量,而Z轴是这两个变量的计数。查看用于计算和显示的包hexbin,或例如用于显示的ggplot的stat\u bin2dstat\u binhex。你得到2个空间坐标,这是你的屏幕或纸张所能做的,再加上第三个彩色编码的维度 请注意,这与这个问题完全相同(但第三维在空间上进行了讨论)。该软件包有一个函数hist3d(实际上不在文档中,但您可以调用它并查看代码) 虽然这是hist3d,但对

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


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

查看用于计算和显示的包hexbin,或例如用于显示的ggplot的
stat\u bin2d
stat\u binhex
。你得到2个空间坐标,这是你的屏幕或纸张所能做的,再加上第三个彩色编码的维度

请注意,这与这个问题完全相同(但第三维在空间上进行了讨论)。

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

虽然这是hist3d,但对我来说,它显示的是三维的二维直方图(输入=x,y)

如果这是您想要的,这里是代码(来自rgl):

>hist3d
函数(x,y=NULL,nclass=“auto”,alpha=1,col=“#ff0000”,scale=10)
{

保存您可以使用基于tucson函数的下一个函数在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)

my_hist3d此问题更适合交叉验证。我已将其标记为版主注意。我必须承认我不理解结束语。我认为此问题很容易回答与软件相关的问题(我理解这是此处的主题):例如,看一看包装盒hexbin或ggplot的
stat\u bin2d
/
stat\u binhex
。你可以得到2个空间坐标,这是你的屏幕或纸张所能做的,再加上第三个彩色编码的维度。也就是说,它可能更值得作为的副本关闭(然而,这3个维度纯粹是在空间上讨论的)。顺便说一下:我将两个变量X和Y及其Z中的计数称为2d直方图。@cbeleites:是的,问题已经重新开始(请参阅修订版)。如果这是重复的,那么我们应该关闭它。如果另一个问题缺少详细信息,请随意在那里添加答案。你考虑过热图吗?pheatmap包可以让它们非常好。只是好奇:线程是重新打开的还是我弄错了?
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)
}
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)