R:绘制3D直方图

时间:2022-04-15 14:59:50

I have a 3 D array. The heads of the columns are "height", "weight", and "age". How can I plot a 3 D histogram using hist3d or any other available function ?

我有一个3D阵列。列的头部是“高度”,“重量”和“年龄”。如何使用hist3d或任何其他可用功能绘制3D直方图?

file<-read.csv(file.choose(),TRUE,"")
x <- file$height 
y <- file$weight
z<- file$age
xlab="Height"
ylab="Weight"
zlab="Age"

I started with this code but then I got stuck on how to draw a 3 D histogram. Thak you for your precious time

我从这段代码开始,但后来我陷入了如何绘制3D直方图的问题。感谢你的宝贵时间

1 个解决方案

#1


dat <- read.table(header=TRUE,
text="
X Y Latency 
0 0 461 
0 1 10295 
0 2 0 
0 3 0 
1 0 169 
1 1 7089 
1 2 4310 
1 3 0")

What you're looking for is technically a 3D bar plot, not a histogram (which would be a summary of the number of discrete points falling within a specified (X,Y) bin).

您正在寻找的是技术上的3D条形图,而不是直方图(这将是指定(X,Y)区域内离散点数量的摘要)。

Here's one solution:

这是一个解决方案:

library("epade")
with(dat,
   bar3d.ade(xticks=unique(X),yticks=unique(Y),
            matrix(Latency,ncol=2),
                 col="gray",alpha=0.5))

Or, modifying from demo("hist3d",package="rgl"):

或者,从demo(“hist3d”,package =“rgl”)修改:

library("rgl")
barplot3d_0 <- function(x,y,z,alpha=1,topcol="#ff0000",sidecol="#aaaaaa")
 {
   save <- par3d(skipRedraw=TRUE)
   on.exit(par3d(save))

  x1 <- c(rep(c(x[1],x[2],x[2],x[1]),3),rep(x[1],4),rep(x[2],4))
  z1 <-c(rep(0,4),rep(c(0,0,z,z),4))
  y1 <-c(y[1],y[1],y[2],y[2],rep(y[1],4),
          rep(y[2],4),rep(c(y[1],y[2],y[2],y[1]),))
  x2 <-c(rep(c(x[1],x[1],x[2],x[2]),2),
        rep(c(x[1],x[2],rep(x[1],3),rep(x[2],3)),))
  z2 <-c(rep(c(0,z),4),rep(0,8),rep(z,8) )
  y2 <-c(rep(y[1],4),rep(y[2],4),
        rep(c(rep(y[1],3),rep(y[2],3),y[1],y[2]),2) )
  quads3d(x1,z1,y1,col=rep(sidecol,each=4),alpha=alpha)
  quads3d(c(x[1],x[2],x[2],x[1]),rep(z,4),c(y[1],y[1],y[2],y[2]),
            col=rep(topcol,each=4),alpha=1) 
  lines3d(x2,z2,y2,col="#000000")
 }
 barplot3d <- function(x,y,z,aspect=c(1,1,1)) {
    dx <- diff(unique(sort(x)))[1]
    dy <- diff(unique(sort(y)))[1]
    mapply(function(x,y,z) {
               barplot3d_0(c(x-dx/2,x+dx/2),
                           c(y-dy/2,y+dy/2),
                           z)
           },x,y,z)
    aspect3d(aspect)
}
with(dat,barplot3d(X,Y,Latency))
axes3d()
##  Z/Y confusion can probably be sorted out ...
title3d(xlab="X",zlab="Y",ylab="Latency")

#1


dat <- read.table(header=TRUE,
text="
X Y Latency 
0 0 461 
0 1 10295 
0 2 0 
0 3 0 
1 0 169 
1 1 7089 
1 2 4310 
1 3 0")

What you're looking for is technically a 3D bar plot, not a histogram (which would be a summary of the number of discrete points falling within a specified (X,Y) bin).

您正在寻找的是技术上的3D条形图,而不是直方图(这将是指定(X,Y)区域内离散点数量的摘要)。

Here's one solution:

这是一个解决方案:

library("epade")
with(dat,
   bar3d.ade(xticks=unique(X),yticks=unique(Y),
            matrix(Latency,ncol=2),
                 col="gray",alpha=0.5))

Or, modifying from demo("hist3d",package="rgl"):

或者,从demo(“hist3d”,package =“rgl”)修改:

library("rgl")
barplot3d_0 <- function(x,y,z,alpha=1,topcol="#ff0000",sidecol="#aaaaaa")
 {
   save <- par3d(skipRedraw=TRUE)
   on.exit(par3d(save))

  x1 <- c(rep(c(x[1],x[2],x[2],x[1]),3),rep(x[1],4),rep(x[2],4))
  z1 <-c(rep(0,4),rep(c(0,0,z,z),4))
  y1 <-c(y[1],y[1],y[2],y[2],rep(y[1],4),
          rep(y[2],4),rep(c(y[1],y[2],y[2],y[1]),))
  x2 <-c(rep(c(x[1],x[1],x[2],x[2]),2),
        rep(c(x[1],x[2],rep(x[1],3),rep(x[2],3)),))
  z2 <-c(rep(c(0,z),4),rep(0,8),rep(z,8) )
  y2 <-c(rep(y[1],4),rep(y[2],4),
        rep(c(rep(y[1],3),rep(y[2],3),y[1],y[2]),2) )
  quads3d(x1,z1,y1,col=rep(sidecol,each=4),alpha=alpha)
  quads3d(c(x[1],x[2],x[2],x[1]),rep(z,4),c(y[1],y[1],y[2],y[2]),
            col=rep(topcol,each=4),alpha=1) 
  lines3d(x2,z2,y2,col="#000000")
 }
 barplot3d <- function(x,y,z,aspect=c(1,1,1)) {
    dx <- diff(unique(sort(x)))[1]
    dy <- diff(unique(sort(y)))[1]
    mapply(function(x,y,z) {
               barplot3d_0(c(x-dx/2,x+dx/2),
                           c(y-dy/2,y+dy/2),
                           z)
           },x,y,z)
    aspect3d(aspect)
}
with(dat,barplot3d(X,Y,Latency))
axes3d()
##  Z/Y confusion can probably be sorted out ...
title3d(xlab="X",zlab="Y",ylab="Latency")