Search code examples
rggplot2histogramaxes

Adding breaks to count (y axis) of a histogram according to the count min-max range in R?


I have a ggplot histogram plot. On the x axis I have a factor variable (1,2,3,4,..)

On the y axis I have count.

I want my y axis to be from minimum count to maximum count, by 1.

I am playing with scale_y_discrete but I can't take min(count), max(count) and add by = 1.

Please advise.

df <- structure(list(user_id = c(1L, 1L, 3L, 3L, 4L, 4L, 4L, 6L, 8L, 
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), obs_id = c(1L, 
30L, 133L, 134L, 144L, 160L, 162L, 226L, 272L, 273L, 274L, 275L, 
276L, 299L, 307L, 322L, 323L, 324L, 325L, 326L, 327L, 328L), 
    n = c(6L, 6L, 10L, 6L, 11L, 11L, 12L, 6L, 3L, 2L, 5L, 2L, 
    3L, 5L, 12L, 11L, 25L, 7L, 5L, 2L, 5L, 17L)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -22L), vars = "user_id", drop = TRUE, .Names = c("user_id", 
"obs_id", "n"), indices = list(0:1, 2:3, 4:6, 7L, 8:12, 13:21), group_sizes = c(2L, 
2L, 3L, 1L, 5L, 9L), biggest_group_size = 9L, labels = structure(list(
    user_id = c(1L, 3L, 4L, 6L, 8L, 9L)), class = "data.frame", row.names = c(NA, 
-6L), vars = "user_id", drop = TRUE, .Names = "user_id"))

Solution

  • You can make a function for breaks that takes the limits of axis as the argument.

    From the documentation of scale_continuous, breaks can take:

    A function that takes the limits as input and returns breaks as output

    Here is an example, where I go from 0 to the maximum y axis limit by 1. (I use 0 instead of the minimum count because histograms start at 0.)

    The x in the function is the limits of the axis in the plot as calculated by ggplot() or as set by the user.

    byone = function(x) {
         seq(0, max(x), by = 1)
    }
    

    You can pas this function to breaks in scale_y_continuous(). The limits are pulled from directly from the plot and passed to the first argument of the function.

    ggplot(df, aes(user_id)) +
         geom_histogram() +
         scale_y_continuous(breaks = byone)
    

    enter image description here