Home > Software engineering >  correcting number of bins in ggplot2 histogram
correcting number of bins in ggplot2 histogram

Time:04-15

I'm trying to create a histogram with ten bins, each with a value of 10. For example, 0-10, 10-20, etc. My histogram is omitting my last bin (90-100), even though I specified 10 bins. Instead, I have nine. There aren't any values that would fall into the 90-100 bin, but I would still like it to be on the x axis. How do I force ggplot2 into keeping the 90-100 bin?

This is the code I used:

habitatdata$title <- "Available" 
hplot3 <- ggplot(ist1, aes(x=ist))   
  geom_histogram(bins = 10, binwidth = 10, colour = "white", fill = "snow3", boundary = 0)  
  theme_classic()  
  scale_x_continuous(breaks = seq(0, 100, by = 10))  
  theme(panel.grid.minor.x = element_blank())  
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = 
        "black", size=1.0))  
        ggtitle("Impervious Surface at Occupied Versus Available Sites")  
        xlab("Impervious Surface (%)")   
        ylab("Count")  
        facet_grid(. ~ title)

And this is what it's kicking back: enter image description here

The 90-100 bin is missing.

CodePudding user response:

The axis is automatically fit to the range of the data. To extend it, add limits = c(0, 100) to scale_x_continuous.

  • Related