Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting the x-Axis and Bins when Making a Histogram with Ggplot2

Tags:

r

ggplot2

I,n new to histograms in ggplot2 and feel like I'm missing something obvious. Using the the data below, I'm trying to create a simple histogram. All I want to do is have the x-axis count by 5, so 0-5-10-15-20-25-30...etc...100. So the bins will divide the age group by 5. I'm thinking maybe I have to set boundaries or something? I'm not sure why the example below creates some bars wider than others, and why the bars don't line up properly on the x-axis.

I tried to find similar questions that have been asked previously but I'm still confused,especially between "break=" and "binwidth" and boundaries, etc.

I would prefer an answer that uses ggplot 2.0.

 Age<-c(18,82,65,89,10,44,46,23,10,15,22,33,34,30,50,60)
 Sex<-c("M","F","F","M","F","M","F","M","F","M","F","M","F","F","F","M")
 Df<-data.frame(Sex,Age)

 ggplot(Df,aes(Age))+geom_histogram(breaks=seq(0,100,by=10),aes  (fill=..count..))
like image 640
Mike Avatar asked Dec 18 '22 11:12

Mike


1 Answers

binwidth controls the width of each bin while bins specifies the number of bins and ggplot works it out.

Depending on how much control you want over your age buckets this may do the job:

ggplot(Df, aes(Age)) + geom_histogram(binwidth = 5)

Edit: for closer control of the breaks experiment with:

+ scale_x_continuous(breaks = seq(0, 100, 5))

To label the actual spans, not the middle of the bar, which is what you need for something like an age histogram, use something like this:

ggplot(Df, aes(Age)) +
geom_histogram(
    breaks = seq(10, 90, by = 10), 
    aes(fill = ..count..,
    colour = "black")) + 
scale_x_continuous(breaks = seq(10, 90, by=10))
like image 156
Jacob Avatar answered Mar 29 '23 23:03

Jacob