简体   繁体   English

使用 Plot3D 库中的 Hist3D 在 R 中创建 3d 直方图时,箱不与轴刻度对齐

[英]When creating a 3d histogram in R, using Hist3D from the Plot3D library, the bins don't line up with axis ticks

As my title suggests I am trying to create a 3D histogram using the Plot3D package.正如我的标题所示,我正在尝试使用 Plot3D package 创建 3D 直方图。 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].正如您在图像中观察到的,直方图的 bin 延伸到 [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).也就是说,我希望图表能够正确表示所有数据点的 x 和 y 值介于 0 和 1 之间。现在查看 plot,可能会让人相信包含原点的 bin,例如,也可能包含数据点 (-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例如,如果我尝试使用命令 plot 东西

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.如果您不定义xy范围, 0 - 1范围是hist3D的默认行为。

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 ):如果您使用箱的中间( 0.1 0.3 0.5 0.7 0.9 )定义xy arguments ,则会得到预期的结果:

hist3D(x = seq(0.1,0.9,0.2),y=seq(0.1,0.9,0.2),z=xy_table, ticktype = "detailed")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM