Search code examples
rhistogramlogarithm

histogram in r on log-log scale displays only a limited amount of values


I want to generate histograms on a log-log scale. I am using R 2.15.2. Following the post Histogram with Logarithmic Scale my minimal code example looks like:

a <- rlnorm(1000)
hist.a <- hist(a, plot = FALSE)
plot(hist.a$count, log = "xy")

In my case the resulting histogram has its highest value at about 15. However, max(a) shows the highest value is above that.

The question is: How can I make it display all the values of a?


Solution

  • When you use in function plot() only object hist.a$count on the x axis you get just number corresponding to length of your object - they are not actual numbers of a values. You can add actual values later with axis() and taking labels= from hist.a$mids.

    set.seed(5555)
    a <- rlnorm(1000)
    hist.a <- hist(a, plot = FALSE)
    
    hist.a
      $breaks
      [1]  0  5 10 15 20 25 30 35 40
    
      $counts
      [1] 955  34   6   2   2   0   0   1
    
      $density
      [1] 0.1910 0.0068 0.0012 0.0004 0.0004 0.0000 0.0000 0.0002
    
      $mids
      [1]  2.5  7.5 12.5 17.5 22.5 27.5 32.5 37.5
    
      $xname
      [1] "a"
    
      $equidist
      [1] TRUE
    
      attr(,"class")
      [1] "histogram"
    
    plot(hist.a$count, log = "xy",xaxt="n",type="h",lwd=10,lend=2)
    axis(1,at=1:length(hist.a$mids),labels=hist.a$mids)