Search code examples
rhistogramaxis

Customize x axis range


I want to change the x axis range of my histogram. How can I remove all hours for which there is no data from the x axis?

enter image description here Help would be appreciated.


Solution

  • One way you can artificially squeeze the values together is to change the bin size. Here is a randomly generated extreme bimodal distribution like yours. In each bar is 500 counts of data:

    hist(rbinom(n=1000,
           size=1,
           prob = .50))
    

    enter image description here

    If we simply reduce the number of bins that the values go into, it reduces the gap in between the values and labeling below where they lie:

    hist(rbinom(n=1000,
                size=1,
                prob = .50),
         breaks = 3)
    

    enter image description here

    The problem lies in that data won't be represented accurately depending on what you're doing, so I would suggest explaining why you made that decision if you share this elsewhere.

    A less extreme example below with various counts but still gaps:

    hist(rbinom(n=1000,
            size=10,
            prob = .20))
    

    enter image description here And here if we shift the bins again, they will distribute in the plot with less gaps, making it more right skewed but more distribution within the bars:

    hist(rbinom(n=1000,
                size=10,
                prob = .20),
         breaks = 6)
    

    enter image description here

    As far as I'm aware there isn't really a way to just remove the values in between the plot. For one, its a distribution plot, so whatever values you remove a priori will just redistribute when you generate the plot. Second, if there is in fact a way, and I wouldn't doubt there is, your x axis would look strange, as you would have a bunch of missing data unaccounted for, looking like this:

    enter image description here

    Therefore, changing the bins, showing where the data is actually allocated, is probably the best choice.