简体   繁体   English

R:绘制3D直方图

[英]R: plot a 3 D histogram

I have a 3 D array. 我有一个3D阵列。 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 ? 如何使用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. 我从这段代码开始,但是后来我被困在如何绘制3D直方图上。 Thak you for your precious time 为您的宝贵时间致敬

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)bin中的离散点数的汇总)。

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

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

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