Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make 3D histogram in R

Tags:

plot

r

histogram

3d

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!!

like image 471
José Ricardo Avatar asked May 31 '15 22:05

José Ricardo


People also ask

How do you make a 3D graph in R?

Creating 3D Plots in R Programming – persp() Function 3D plot in R Language is used to add title, change viewing direction, and add color and shade to the plot. The persp() function which is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane.

Can you make 3D graphs in R?

R allows to build three dimensional charts, mainly thanks to the rgl package. Even if 3D is often a bad practice, it can be useful in specific situation.

How do you plot a 3D histogram in Python?

MatPlotLib with PythonSet the figure size and adjust the padding between and around the subplots. Create a new figure or activate an existing figure using figure() method. Add an axes to the cureent figure as a subplot arrangement. Create x3, y3 and z3 data points using numpy.


1 Answers

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

like image 199
Forrest R. Stevens Avatar answered Oct 02 '22 18:10

Forrest R. Stevens