As my title suggests I am trying to create a 3D histogram using the Plot3D package. The following is a minimum working (or rather not working) example of the problem I'm having:
library(plot3D)
x = runif(10000)/2
y=runif(10000)
cuts = c(0, 0.2, 0.4, 0.6, 0.8, 1)
x_cut = cut(x, cuts)
y_cut = cut(y, cuts)
xy_table = table(x_cut, y_cut)
hist3D(z=xy_table, ticktype = "detailed")
This produces the following image:
As you can observe in the image, the bins of the histogram extend outside of [0,1]x[0,1]. Is there anyway I can force the bins to line up exactly with the ticks on the axis? That is, I would like the graph to correctly represent that all data points have x and y values between 0 and 1. Looking at the plot now, one could be led to believe that the bin containing the origin, for example, might also contain the data point (-0.1, 0). This cannot happen in the data I am trying to display and I need the axis to convey that.
I've spent all day fiddling with the various axis parameters and whatnot but cannot get it to work. For example if I try to plot things using the command
hist3D(z=xy_table, ticktype = "detailed", xlim=c(0,1), ylim=c(0,1))
Than I get something even worse:
I feel like I must be missing something obvious but I'm just not seeing what it is. If anyone has an answer please do share. And thank you for taking the time to read my question.
0 - 1
range is the default behaviour of hist3D
if you don't define x
and y
ranges.
You get the expected result if you define x
and y
arguments using te middle of the bins ( 0.1 0.3 0.5 0.7 0.9
):
hist3D(x = seq(0.1,0.9,0.2),y=seq(0.1,0.9,0.2),z=xy_table, ticktype = "detailed")