Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

plot - How to make 3D histogram in R

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

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

Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

3D2D


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...