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..))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With