Adrian
Adrian

Reputation: 9793

R: how to use hist() so that the bars are not centered at the value

mydat <- c(-0.668273585044951, -0.668349982287781, -0.668310321831499, 
-0.668340631422403, -0.668289165486385, -0.668291483851571, -0.668248500627415, 
-0.668268688870632, -0.668268351574953, -0.668257756588694, -0.668248478450076, 
-0.668328152612909, -0.668290376266347, -0.668259884873199, -0.668336672777167, 
-0.66825928480685, -0.66824973313942, -0.668267624202257, -0.668257723530567, 
-0.66824923987732, -0.66827032896674, -0.668296341784685, -0.668269768188303, 
-0.668317368549213, -0.668252896596478, -0.668259320312454, -0.668358156147855, 
-0.66825880651865, -0.668266956160338, -0.668256850457009, -0.668248611381841, 
-0.668254104957217, -0.668261414353884, -0.668287765972581, -0.668298912305036, 
-0.668249596880931, -0.668248527406481, -0.668248522170991, -0.668257508692103, 
-0.668249113734722, -0.668290173078401, -0.668249175742675, -0.668320493819355, 
-0.668281236636089, -0.66825021985808, -0.668337981787248, -0.668252181034097, 
-0.668251699984127, -0.668328704629881, -0.668297709662365, -0.668343189322021, 
-0.668259570734301, -0.668264001643703, -0.668253905874522, -0.668289644511903, 
-0.668264044020724, -0.66826754890621, -0.668358431773946, -0.668248494061212, 
-0.668319635038578, -0.668250207208146, -0.668332762567503, -0.668250151279497, 
-0.66825077635007, -0.668259787977316, -0.668249253537775, -0.668299308339268, 
-0.668252348743164, -0.668314437014813, -0.668356526573943, -0.668250417392189, 
-0.668249626422078, -0.66825978956337, -0.668262741366315, -0.668251621645908, 
-0.668396319087419, -0.668267793877638, -0.668323245408643, -0.668272920904538, 
-0.668258917039086, -0.668250552455484, -0.668365803143064, -0.668249334840926, 
-0.668274369881539, -0.668268739577508, -0.66827144082708, -0.668312908978672, 
-0.668328199044459, -0.668300853703848, -0.668265592444912, -0.668390761110759, 
-0.668270647634556, -0.668357480805416, -0.668248487606074, -0.668248495246697, 
-0.668259841639527, -0.668348041989136, -0.668249924780612, -0.6683072047334, 
-0.668250109800024)
hist(mydat, breaks = 10)
abline(v = max(mydat), col = "blue")

enter image description here

In my plot, I'm adding a blue vertical line to indicate where the maximum value of mydat is. In this example, it is at -0.6682485. However, because the bars in hist are centered at the values in my data set, I have a bar that is centered at exactly -0.6682485. Therefore, even when I add the blue line, which is supposed to represent the maximum value of mydat, it may not look it because half of the last bar is to the right of the blue line. So it may convey a misleading message that the blue line is not the maximum.

Is there a way to not center the bars at the values in the data set? I just want to make sure that when I plot the blue line, all the bars are to the left of it, signifying that the blue line indicates the maximum value.

Upvotes: 0

Views: 318

Answers (1)

maydin
maydin

Reputation: 3755

Histograms do not work as you expected. Because they draw an interval around the determined means which are changing with respect to the number of bins. If the bin number goes to infinity, then you may expect the maximum point of the histogram equals the maximum of your data.

However if you run the code,

my_hist <- hist(mydat, breaks = 10 ,plot=FALSE)

my_hist

$breaks
[1] -0.66840 -0.66838 -0.66836 -0.66834 -0.66832 -0.66830 -0.66828 -0.66826 -0.66824

You get the break points. As you can see, the maximum point is -0.66824 in here. If you really want to show the max point at the right hand side, you can run,

hist(mydat, breaks = 10)

abline(v =max(my_hist$breaks), col = "blue")

which gives, enter image description here

Note that, this is the calculated max wrt the bin number, not the max of your data.

Upvotes: 2

Related Questions