ggplot2: how to align the bars of a histogram with the x axis?

前端 未结 3 1754
攒了一身酷
攒了一身酷 2020-12-03 14:46

Consider this simple example

library(ggplot2)
dat <- data.frame(number = c(5, 10, 11 ,12,12,12,13,15,15))
ggplot(dat, aes(x = number)) + geom_histogram()
         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 15:06

    This worked for me

    + scale_x_continuous(limits = c(0, NA)) 
    

    From ?scale_x_continuous, limits is:

    One of:

    • NULL to use the default scale range

    • A numeric vector of length two providing limits of the scale. Use NA to refer to the existing minimum or maximum

    • A function that accepts the existing (automatic) limits and returns new limits Note that setting limits on positional scales will remove data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see coord_cartesian()).

提交回复
热议问题