rggplot2axis-labelsdiscrete

R: Skip labels in discrete x axis


I am trying to produce something akin to a histogram of incomes with ggplot2. However, because I am not trying to show the frequency in the y-axis but a calculated variable from my data.frame, I figured I had to use geom_bar() and assign buckets as a new factor variable in my data.frame. Now I would like to show all bars I have determined a bucket for, but not show all labels on the x-axis. I figured there must be some way to do this with the breaks = in scale_x_discrete(), but couldn't figure out how to get this to just take "show me every second label" or something like that.

Would anyone have a suggestion for how to just show every second label on the x-axis for a discrete x-axis based upon a factor variable?

(Thank you very much for anyone's help and sorry, if I'm throwing around the wrong terms here. I'm new to R and just trying to describe everything as accurately as I can.)


Solution

  • You can provide function to the breaks argument that can handle special break rules. In the example below, we're choosing all odd breaks by recycling c(TRUE, FALSE) as a subset operation.

    library(ggplot2)
    
    ggplot(mpg, aes(class)) +
      geom_bar() +
      scale_x_discrete(breaks = function(x){x[c(TRUE, FALSE)]})
    

    Some more explanation:

    The logical index is repeated until it matches the length of the vector it indexes. If the logical index is c(TRUE, FALSE) if will keep repeating this, so it effectively picks up every odd element of the vector x because the length 2 index is repeated.

    x <- 1:9
    
    x[c(TRUE, FALSE)]
    #> [1] 1 3 5 7 9
    

    So when you make a length 3 index, this will get repeated 3 times.

    x[c(TRUE, FALSE, TRUE)]
    #> [1] 1 3 4 6 7 9
    

    If you want to replicate what is going on under the hood, here is how you'd do it.

    i <- rep_len(c(TRUE, FALSE), length(x))
    
    x[i]
    #> [1] 1 3 5 7 9