user1738753
user1738753

Reputation: 636

ggplot2 -- different bin size for histogram

I hope someone will be able to direct me into the correct way. I am trying produce around ~100 histogram, so the solution for my problem has to be easily reproducible.

I have a vector of values that ranges from 0.0000 (zero values) to an upper bound of 1.0000. There are a significant amount of 0.0000 values, and the rest of the values are more spread out.

I want to now create a histogram that has varying bin sizes. I want to have a bin for all of the zero values, and then the bin size should be 0.005

Here is some reproducible code that illustrates my actual data:

per_value <- rbeta(200, 1, 1, ncp = 0.3)
percent_change <- c(0,0,0,0,0, rbeta(195, 1, 1, ncp = 0.1))

valua_chg <- data.frame(cbind(per_value, percent_change))

ggplot(valua_chg, aes(x=percent_change)) + 
   stat_bin(breaks=c(0, seq(0.001,1.0, by=0.005)))

Here is the picture created by the code:

enter image description here

The problem is that the bin size for the zero bin is extremely small, and not the same size as the other bins. Does anyone know how to fix this.

Thank you!

Upvotes: 1

Views: 2884

Answers (1)

Marius
Marius

Reputation: 60180

The quickest, simplest way to do this is to just set the lowest break to -0.004, so that bin containing the zeros has the same width of 0.005. The bin won't be centered right on zero, but if your data is bound between 0 and 1, it should be OK:

ggplot(valua_chg, aes(x=percent_change)) + 
  stat_bin(breaks=c(-0.004, seq(0.001,1.0, by=0.005)))

Upvotes: 2

Related Questions