简体   繁体   English

如何在R中制作3D直方图

[英]How to make 3D histogram in R

This is my goal: Plot the frequency of y according to x in the z axis.这是我的目标:根据z轴上的x绘制y的频率。

These are my problems: I have a two columns array ( x and y ) and need to divide x into classes (p.ex. 0.2 ou 0.5) and calculate the frequency of y for each class of x .这些是我的问题:我有一个两列数组( xy ),需要将x分成几类(p.ex. 0.2 ou 0.5)并计算每个x类的y频率。 The plot should appear like a xy plot in the "ground" plan and the frequency in the z axis.该图应该像“地面”平面中的xy图和z轴中的频率一样。 It could be like a surface or a 3D histogram.它可以像一个表面或一个 3D 直方图。 I tried to make it using the hist3D function of plot3D package but I don't know what I am doing wrong.我试图使用plot3D包的hist3D函数来制作它,但我不知道我做错了什么。

This is an example of what I am trying to do:这是我正在尝试做的一个例子:

https://www.safaribooksonline.com/library/view/r-data-visualization/9781783989508/ch06s05.html https://www.safaribooksonline.com/library/view/r-data-visualization/9781783989508/ch06s05.html

Thanks!!谢谢!!

Using some simulated data, this should get you what you want.使用一些模拟数据,这应该可以满足您的需求。 The key is that you have to create your bivariate bins, accomplished using the cut() function.关键是您必须使用cut()函数创建双变量箱。 Then treating the binned factors as levels we can then count the combinations of each factor level using the table() function like below:然后将分箱因子视为级别,然后我们可以使用table()函数计算每个因子级别的组合,如下所示:

library(plot3D)

##  Simulate data:
set.seed(2002)
x <- rnorm(1000)
y <- rnorm(1000)

##  Create cuts:
x_c <- cut(x, 20)
y_c <- cut(y, 20)

##  Calculate joint counts at cut levels:
z <- table(x_c, y_c)

##  Plot as a 3D histogram:
hist3D(z=z, border="black")

##  Plot as a 2D heatmap:
image2D(z=z, border="black")

3D二维

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

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