-1

I want to make two histograms with two types data from the same population. One type of data has a broader range than the other and therefore that particular histogram has fewer bins with greater frequency than the other. Is there a general way to make the bin size of the two histograms the same?

n=377 #just a number
df <- data.frame(v.1=sample(2:10,n, replace =T),v.2=sample(30:130,n,replace = T))
H.1 <- hist(df$v.1)
H.2 <- hist(df$v.2,ylim=c(0,max(H.1$counts)))
str(H.1)
str(H.2)
Valtyr
  • 123
  • 1
  • 12

1 Answers1

2

Set breaks argument as a vector. you can control bin size precisely.

From ?hist:

breaks: one of:

        • a vector giving the breakpoints between histogram cells,

you may use breaks=seq(1,11,2) for v.1, and breaks=seq(29,131,2) for v.2 so bin size for both is 2

however, be careful with the range of breaks. it must cover the range of your data. for example, if you do breaks=seq(1,11,4) and breaks=seq(29,131,4), you might get into trouble. seq(1,11,4) is 1, 5, 9 and does not cover 10. so you may want seq(1,13,4); similarly, seq(29,133,4).