Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Make Histogram Bins in R-Studio Only Use Integers

Tags:

r

histogram

I am attempting to create several histograms that display the effects a drug has on the frequency of heart attacks.

Currently, R is organizing my data into the bins [0 - 0.5, 0.5 - 1.0, 1.0 - 1.5, etc.], but I would like for it to only use integer values: [0 - 1, 1 - 2, 2 - 3, etc.].

I have tried using the xaxt="n" argument and the axis() function. They "worked," but they did not solve the problem above. I also tried to use breaks=seq(0,5,l=6), but this converted my y-axis from frequency into density.

Here is the code for my latest two attempts:

hist(fourTrials$red_5, breaks=5, right = FALSE, 
      xlab = "Number of Heart Attacks", 
      xlim = c(0, 4), ylim = c(0,4), 
      main = "Experimental Group 1, n = 400", col = "light blue")

hist(fourTrials$red_5, breaks=seq(0,5,l=6), freq = F, right = FALSE, 
      xlab = "Number of Heart Attacks", 
      xlim = c(0, 4), ylim = c(0,4), 
      main = "Experimental Group 1, n = 400", col = "light blue",yaxs="i",xaxs="i")

In this image, I want the bars to occupy the whole bin space. For example, the third bar should occupy the 2-3 bin.

Thanks for any help!

like image 665
Guy Cerretti Avatar asked Oct 21 '25 16:10

Guy Cerretti


1 Answers

I believe that what you want is:

hist(fourTrials$red_5, breaks=0:4, freq = TRUE, right = FALSE, 
    xlab = "Number of Heart Attacks", 
    xlim = c(0, 4), ylim = c(0,4), 
    main = "Experimental Group 1, n = 400", 
    col = "lightblue", yaxs="i", xaxs="i")
like image 141
G5W Avatar answered Oct 23 '25 06:10

G5W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!