如何在R中制作3D直方图 [英] How to make 3D histogram in R

查看:61
本文介绍了如何在R中制作3D直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的目标:根据 xz 轴上绘制 y 的频率.

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

这些是我的问题:我有一个两列数组(xy),需要将 x 分成几个类(p.例如 0.2 或 0.5) 并计算 y 为每个 x 类的频率.该图应该看起来像地面"平面中的 x-y 图和 z 轴中的频率.它可以像一个表面或一个 3D 直方图.我尝试使用 plot3D 包的 hist3D 函数来制作它,但我不知道我做错了什么.

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.

这是我正在尝试做的一个例子:

This is an example of what I am trying to do:

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

谢谢!!

推荐答案

使用一些模拟数据,这应该可以满足您的需求.关键是您必须创建双变量 bin,使用 cut() 函数完成.然后将分箱因子视为级别,然后我们可以使用 table() 函数计算每个因子级别的组合,如下所示:

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:

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中制作3D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆