【问题标题】:Hist of a sample - change limits x, y in R样本的历史 - 在 R 中更改限制 x、y
【发布时间】:2020-12-19 17:21:58
【问题描述】:

我为这个历史写了一个代码 -

但是,我需要 x, y 的限制就像这张图中一样-

当我在我的代码中更改 x,y 的限制时,历史就会消失。

这是我的代码 -

hist(MySample, breaks= 100, prob = TRUE )

我的数据是这样的 -

    > dput(my_vec)
c(7.16523478153752, 5.66659652818595, 4.47575534893755, 4.84970857977856, 
15.2276296414708, -0.573093658844655, 4.97980673868322, 2.73969325233614, 
5.14683035133365, 10.1221488713611, 9.01656611721311, 65.711819422978, 
5.52057043354834, 6.30674880627702, 8.67771771267678, 5.2528503049587, 
3.50395623925858, 4.24774012371174, 11.4137624410312, -48.1722033880239, 
-0.376400642113356, 5.76475359419462, -27.353313803102, 4.09682042042852, 
5.03373747558625, 3.8261660769812, 4.43580895525249, 4.22242932760446, 
4.44905425775097, 4.98475525084258, 3.6416524979406, 3.81767927422987, 
-93.2141354888589, 5.01103555428068, 5.38206564752185, 3.0296536606134, 
86.676038167071, 3.76536864898752, 7.26590572567999, 4.63759338498908, 
-17.2259677581435, 4.90903933854569, 4.43042810097151, 5.41519101700693, 
7.01553803666926, 5.05370712380939, 5.22896180532498, 3.92429687923716, 
5.46452912130986, 4.50524864464907, 6.13119216629816, 6.931011365041, 
5.70039361940988, 6.12470771804837, 6.66119827017415, -4.26865103703534, 
4.77160581324527, 7.91297525486072, 7.04882594451997, -98.2224152538262, 
6.22663920777969, 5.77535246011091, -9.91868097075743, 7.77803716672203, 
-10.1297033914588, 4.56699263921898, 8.56120643036614, 2.28239965661689, 
5.6212927060249, 5.42419784358529, 5.3654428914847, 3.89730116338776, 
3.93504254066386, 4.38168766024675, 3.00038017933155, 4.81884527713388, 
4.45257297902316, -3.50516232920359, 6.07365636649988, 4.26195094225287, 
4.73753258557777, 0.807607628402986, 3.93740907315333, 3.08283749617447, 
3.77379774203008, 2.56562208889432, -19.6532587812275, 8.00379422844706, 
5.27350155029373, 5.17570743606727, 6.49856446395129, -8.78462344722329, 
4.38775671122269, 4.39685118092286, 3.52571990926583, 7.13834590807858, 
0.724655622024244, 5.72807728660989, 6.58172179467414, 6.22426074825281
)

有什么帮助吗?

【问题讨论】:

  • 请提供一个可重现的示例,该示例至少包括您的对象MySample 通过dput 或类似的。另见:stackoverflow.com/questions/5963269/…
  • 我怀疑您遇到的问题是,当您的休息时间设置为 100 时,概率密度在 4 和 5.4 之间没有。您的数据范围太高。如果您需要其他帮助,请edit 您的帖子并将dput(MySample) 的输出粘贴到您的问题中。
  • 好的,我的数据是包含 100 个数字的 txt 文件,其中大部分在 4 到负 4 之间,我需要对一个 1000 次迭代的数字进行采样(带替换)

标签: r random sample


【解决方案1】:

我认为 Ian Campbell 是对的,而您的问题是,对于 breaks = 100,感兴趣的整个范围 (xlim = c(4, 5.4)) 位于直方图的单个条形内,看起来并不多:

hist(my_vec, probability = TRUE, xlim = c(4, 5.4), breaks = 100)

产生的这个情节可能不是你想要的:

所以一个简单的解决方案是修复breaks 参数。当您只给出一个数字(即breaks = n)时,它会将 x 值的范围分解为n 箱。因此,当您放大直方图的一小部分时,您需要增加 breaks 值以保持图中 bin 的相对宽度。

hist(my_vec, probability = TRUE, xlim = c(4, 5.4), breaks = 2000)
lines(density(my_vec), col = "blue")

产生更接近你分享的例子的东西:

当然,除非您的数据完全相同,否则您不能期望您的图看起来完全像您粘贴的那样。

如文档 (help(hist)) 和其他一些有用的帖子 (12) 中所述,有多种方法可以确保您使用 hist 获得所需的垃圾箱数量和位置。这是一个使用您的示例的通用解决方案:

# desired number of bins in plot
bins <- 14
# desired xmin for plot
plot_min <- 4
# desired xmax for plot
plot_max <- 5.4

# calculate optimal breaks for desired plot output
breaks <- bins*(max(my_vec)-min(my_vec))/(plot_max - plot_min)

# plot with optimal bins
hist(my_vec, probability = TRUE, xlim = c(plot_min, plot_max), breaks = breaks)
lines(density(my_vec), col = "blue")

这将给出完全相同的绘图,但如果您需要更改绘图参数,它很容易调整。

【讨论】:

  • 这是我的数据的一部分 - [1] 7.1652348 5.6665965 4.4757553 4.8497086 15.2276296 -0.5730937 4.9798067 2.7396933 [9] 5.1468304 10.1221489 9.0165661 65.7118194 5.5205704 6.3067488 8.6777177 5.2528503 [17] 3.5039562 4.2477401 11.4137624 -48.1722034 -0.3764006 5.7647536 - 27.3533138 4.0968204 [25] 5.0337375 3.8261661 4.4358090 4.2224293
  • 您可以使用dput(MySample) 导出数据并将结果从控制台复制到您的问题中。
  • > dput(my_vec)C(7.16523478153752,5.66659652818595,4.47575534893755,4.84970857977856,15.2276296414708,-0.573093658844655,4.97980673868322,2.73969325233614,5.14683035133365,10.1221488713611,9.01656611721311,65.711819422978,5.52057043354834,6.30674880627702,8.67771771267678,5.2528503049587,3.50395623925858, 4.24774012371174,11.4137624410312,-48.1722033880239,-0.376400642113356,5.76475359419462,-27.353313803102,4.09682042042852,5.03373747558625,3.8261660769812,4.43580895525249,4.22242932760446,跨度>
  • 看起来你剪裁了输出,只得到了前 28 个值(它以逗号而不是右括号结尾)。
  • 是的,因为我无法将所有值都复制到这里.. 没有足够的字符了
猜你喜欢
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2017-11-15
相关资源
最近更新 更多