How to change bin width inside ggplot?

I am making a histogram with ggplot.

p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) + xlab(xlabel) + ylab(ylabel) + geom_histogram(colour = "darkblue", fill = "white", binwidth=500) 

my x is between 2 and 6580, and I have 2600 data.

I want to build one histogram with different binwidth . Is it possible?

For example, I want to have 8 bars with this width:

 c(180,100,110,160,200,250,1000,3000) 

How can i do this?

+4
source share
2 answers

what about using breaks?

 x <- rnorm(100) ggplot(NULL, aes(x)) + geom_histogram(breaks = c(-5, -2, 0, 5), position = "identity", colour = "black", fill = "white") 

PS Please do not cross messages without explicit notice.

enter image description here

+15
source

Use breaks and position="dodge"

eg:

 ggplot(mtcars,aes(x=hp))+geom_histogram(breaks=c(50,100,200,350),position="dodge") 

You have no data, but for your example:

 p <- ggplot(TotCalc, aes(x=x,y=100*(..count../sum(..count..)))) + xlab(xlabel) + ylab(ylabel) + geom_histogram(colour = "darkblue", fill = "white", breaks=cumsum(c(180,100,110,160,200,250,1000,3000)), position="dodge") 
+2
source

Source: https://habr.com/ru/post/1380190/


All Articles