Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recreate hist() binning in ggplot2 with geom_histogram()

(if you are only interested in the problem, then go to "What if in short?")

What kind of stupid question?

I’m doing work and before that I built all graphics with x and I don’t want to change the style.
And now I need a histogram, but it does not suit me with ggplot2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What do I mean?

I took the width of the column from hist(), so there will be the same number of them
(which can be seen from the graphs),
but in hist() and as I want,
the bars do NOT cross the important/magic number 0.0012,
and in `geom_histogramm’ intersects.

And if it’s short?

How to "shift" histogram bars with ggplot2 so that they do not cross a certain number (0.0012)?
Or, how to make a histogram shorter with "data" from hist() and design with ggplot2?

Here is my code:

# check bin width
  standart_hist <- hist(my_vector, plot = F)
  bw <- standart_hist$breaks[2] - standart_hist$breaks[1]
  
  # create hist with ggplot and bw from standart hist
  gghist <- ggplot(mapping = aes(my_vector)) +
    geom_histogram(
      binwidth = bw,
      color = "black",
      fill = "white"
    ) 

and result:

  1. my hist

my hist

  1. standard hist

standart hist

FIX:

from joran — instead of geom_histogram() use stat_bin() as here:

stat_bin(geom = 'bar',breaks = <breaks vector from hist() output>)

My data:

my_vector <- (0.001201367, 0.001199250, 0.001198337, 0.001199200, 0.001199353, 0.001198439, 0.001202447, 0.001205639, 0.001207056, 0.001209714, 0.001204478, 0.001200064, 0.001199386, 0.001199976, 0.001200569, 0.001204738, 0.001208508, 0.001201491, 0.001200995, 0.001199861, 0.001200242, 0.001196367, 0.001200365, 0.001201807, 0.001194364, 0.001197196, 0.001192705, 0.001196178, 0.001192991, 0.001189777, 0.001194227, 0.001197158, 0.001204336, 0.001201081, 0.001201100, 0.001204755, 0.001198810, 0.001202090, 0.001194370, 0.001188529, 0.001191450, 0.001193616, 0.001195733, 0.001198886, 0.001201353, 0.001206878, 0.001201262, 0.001194806, 0.001196192, 0.001193215, 0.001195030, 0.001198202, 0.001184351, 0.001191890, 0.001192882, 0.001194621, 0.001203256, 0.001204150, 0.001197425, 0.001198002, 0.001196185, 0.001194915, 0.001198281, 0.001201858, 0.001195349, 0.001196401, 0.001205476, 0.001201740, 0.001197276, 0.001189442, 0.001192760, 0.001196846, 0.001201342, 0.001204854, 0.001202979, 0.001203136, 0.001199926, 0.001197398, 0.001199905, 0.001199252, 0.001198486, 0.001197114, 0.001196829, 0.001200228, 0.001199666, 0.001194918, 0.001204005, 0.001201363, 0.001204183, 0.001205889, 0.001204553, 0.001202369, 0.001203922, 0.001197001, 0.001200020, 0.001202672, 0.001201746, 0.001203532, 0.001198699, 0.001200975, 0.001202635, 0.001203121, 0.001190614, 0.001199029, 0.001200372, 0.001193731, 0.001193428, 0.001200259, 0.001195203, 0.001194854, 0.001193173, 0.001198266, 0.001195362, 0.001195252, 0.001201008, 0.001199291, 0.001196653, 0.001200357, 0.001201623, 0.001207463, 0.001199381, 0.001198047, 0.001196305, 0.001200419, 0.001208689, 0.001197434, 0.001193885, 0.001198708, 0.001204741, 0.001204281, 0.001193663, 0.001200234, 0.001203809, 0.001199003, 0.001195127, 0.001192189, 0.001187610, 0.001191390, 0.001200602, 0.001197817, 0.001202045, 0.001203998, 0.001205508, 0.001201051, 0.001202057, 0.001208911, 0.001203928, 0.001202267, 0.001201434, 0.001202647, 0.001210024, 0.001210509, 0.001207881, 0.001206928, 0.001206128, 0.001203866, 0.001202204, 0.001204511, 0.001202310, 0.001197504, 0.001199019, 0.001200713, 0.001204197, 0.001204649, 0.001207965, 0.001201847, 0.001200585, 0.001203446, 0.001195972, 0.001202405, 0.001197182, 0.001191603, 0.001197663, 0.001202259, 0.001201008, 0.001200354, 0.001198090, 0.001193479, 0.001202457, 0.001201156, 0.001196038, 0.001201092, 0.001205488, 0.001212173, 0.001203497, 0.001208846, 0.001198349, 0.001200047, 0.001200799, 0.001206939, 0.001207142, 0.001201970, 0.001202742, 0.001204795, 0.001198463, 0.001201559, 0.001201344, 0.001206085, 0.001205526, 0.001197508)

>Solution :

Using your data, I believe this does what you want:

h <- hist(my_vector)

ggplot(data = data.frame(x = my_vector),aes(x = x)) + 
  stat_bin(geom = 'bar',breaks = h$breaks)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading