0

I wrote a code for this hist -

enter image description here

But, I need that the limits of x, y will be like in this graph-

without the line on the hist.

when I change in my code the limits of x,y the hist dissapear.

this is my code -

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

and my data is this -

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

any help?

John John
  • 15
  • 6
  • 1
    Please provide a reproducible example, that at least includes your object `MySample` via `dput`or similar. See also: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Julian_Hn Dec 19 '20 at 17:33
  • 1
    I suspect the problem you are having is there is no probability density between 4 and 5.4 when your breaks are set to 100. Your data has too high of range. If you need additional help, please [edit](https://stackoverflow.com/posts/65372513/edit) your post and paste the output of `dput(MySample)` into your question. – Ian Campbell Dec 19 '20 at 17:53
  • ok my data is txt file with 100 numbers , most of them are between 4 and minus 4, and I need to sample a number with 1000 iterations (with replace) – John John Dec 19 '20 at 17:58

1 Answers1

0

I think that Ian Campbell was right and that your problem is that with breaks = 100, the entire range of interest (xlim = c(4, 5.4)) sits inside a single bar of the histogram and doesn't look like much:

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

produces this plot which is probably not what you want:

enter image description here

So one easy solution is to fix the breaks argument. When you give just a single number (i.e. breaks = n), it will break the range of x values into n bins. So when you zoom in on a small part of your histogram you need to increase the breaks value to preserve the relative width of the bins in your plot.

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

Produces something closer to the example you shared:

enter image description here

Of course you can't expect your plot to look exactly like the one you pasted unless your data is exactly the same.

As described in the documentation (help(hist)) and several other useful posts (1, 2), there are a number of ways to ensure you get the desired number and position of bins with hist. Here is one generalizable solution using your example:

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

This will give the exact same plot but it's easy to adjust if you needed to change the plotting parameters.

Dan Adams
  • 4,971
  • 9
  • 28
  • this is part of my data - [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 – John John Dec 19 '20 at 19:07
  • You can export your data using `dput(MySample)` and copy the result from the console into your question. – Dan Adams Dec 19 '20 at 19:10
  • > 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, – John John Dec 19 '20 at 19:58
  • It looks like you clipped the output and only got the first 28 values (it ends in a comma instead of a close parentheses). – Dan Adams Dec 19 '20 at 20:09
  • Yes because I cant copy to here all the values.. there is no enough characters left – John John Dec 19 '20 at 20:29
  • Put it in the actual question, then there won't be a limit. Just go back and edit your question. – Dan Adams Dec 19 '20 at 20:45
  • Thank you. I did this – John John Dec 19 '20 at 20:50
  • any help?please – John John Dec 19 '20 at 21:07