Vasile
Vasile

Reputation: 1017

Changing bin width default in ggplot

I would like to compare the distribution of a variable using frequency and percentages histograms. In other words I would like to see how many percentages each bin in frequency histogram represents.

#load data
j<-as.data.frame(Nile)
#frequency plot  
 ggplot(j, aes(x = x))+
 geom_histogram( fill = "black", binwidth = 200, alpha=0.8)+
 scale_x_continuous(breaks =seq(400,1400,200)) 

enter image description here Next I try to create the percentage histogram with the same binwidth

#percentage plot
ggplot(j, aes(x = x), fill = "black", binwidth = 200, alpha=0.8)+
  geom_histogram( aes(y= (..count..)/sum(..count..)*100))+
  scale_x_continuous(breaks =seq(400,1400,200))+
  ylab("Percentages") 

However, it gives me an warning stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. with a different histogram that I expect enter image description here

I tried the following

ggplot(j, aes(x = x), fill = "black", alpha=0.8)+
  geom_histogram( aes(y= (..count..)/sum(..count..)*100))+
  scale_x_continuous(breaks =seq(400,1400,200))+
  ylab("Percentages") +
    stat_bin(bindwidth=200)

However, I get the same second histogram with default binwidth and the same warning

Upvotes: 0

Views: 4186

Answers (2)

black_sheep07
black_sheep07

Reputation: 2368

You spelled binwidth incorrectly. You spelled it as bindwidth

ggplot(j, aes(x = x), fill = "black", alpha=0.8)+
        geom_histogram(aes(y= (..count..)/sum(..count..)*100), binwidth = 200) +
        scale_x_continuous(breaks =seq(400,1400,200)) +
        ylab("Percentages")

Upvotes: 2

Michael O&#39;Neil
Michael O&#39;Neil

Reputation: 21

This seems to be a syntax error. Properly spelling binwidth will fix.

Upvotes: 0

Related Questions