如何在R中制作三维直方图

时间:2021-12-20 13:08:17

This is my goal: Plot the frequency of y according to x in the z axis.

这是我的目标,在z轴上标出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. The plot should appear like a x-y plot in the "ground" plan and the frequency in the z axis. It could be like a surface or a 3D histogram. I tried to make it using the hist3D function of plot3D package but I don't know what I am doing wrong.

这些是我的问题:我有一个两个列数组(x和y),需要将x分为类(p.ex)。然后计算每一类x的y的频率,这个图应该在“地”图中显示为x-y图,在z轴中显示为频率。它可以是一个表面或一个三维直方图。我尝试使用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!!

谢谢! !

1 个解决方案

#1


19  

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. Then treating the binned factors as levels we can then count the combinations of each factor level using the table() function like below:

使用一些模拟数据,这将得到您想要的结果。关键是必须创建双变量容器,使用cut()函数完成。然后将被绑定的因子作为级别来处理,然后我们可以使用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")

如何在R中制作三维直方图如何在R中制作三维直方图

#1


19  

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. Then treating the binned factors as levels we can then count the combinations of each factor level using the table() function like below:

使用一些模拟数据,这将得到您想要的结果。关键是必须创建双变量容器,使用cut()函数完成。然后将被绑定的因子作为级别来处理,然后我们可以使用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")

如何在R中制作三维直方图如何在R中制作三维直方图