Histogram with Logarithmic Scale and custom breaks

后端 未结 7 1871
谎友^
谎友^ 2020-11-27 16:33

I\'m trying to generate a histogram in R with a logarithmic scale for y. Currently I do:

hist(mydata$V3, breaks=c(0,1,2,3,4,5,25))

This giv

相关标签:
7条回答
  • 2020-11-27 16:48

    A histogram is a poor-man's density estimate. Note that in your call to hist() using default arguments, you get frequencies not probabilities -- add ,prob=TRUE to the call if you want probabilities.

    As for the log axis problem, don't use 'x' if you do not want the x-axis transformed:

    plot(mydata_hist$count, log="y", type='h', lwd=10, lend=2)
    

    gets you bars on a log-y scale -- the look-and-feel is still a little different but can probably be tweaked.

    Lastly, you can also do hist(log(x), ...) to get a histogram of the log of your data.

    0 讨论(0)
  • 2020-11-27 16:49

    Here's a pretty ggplot2 solution:

    library(ggplot2)
    library(scales)  # makes pretty labels on the x-axis
    
    breaks=c(0,1,2,3,4,5,25)
    
    ggplot(mydata,aes(x = V3)) + 
      geom_histogram(breaks = log10(breaks)) + 
      scale_x_log10(
        breaks = breaks,
        labels = scales::trans_format("log10", scales::math_format(10^.x))
      )
    

    Note that to set the breaks in geom_histogram, they had to be transformed to work with scale_x_log10

    0 讨论(0)
  • 2020-11-27 16:52

    Run the hist() function without making a graph, log-transform the counts, and then draw the figure.

    hist.data = hist(my.data, plot=F)
    hist.data$counts = log(hist.data$counts, 2)
    plot(hist.data)
    

    It should look just like the regular histogram, but the y-axis will be log2 Frequency.

    0 讨论(0)
  • 2020-11-27 16:59

    It's not entirely clear from your question whether you want a logged x-axis or a logged y-axis. A logged y-axis is not a good idea when using bars because they are anchored at zero, which becomes negative infinity when logged. You can work around this problem by using a frequency polygon or density plot.

    0 讨论(0)
  • 2020-11-27 17:02

    Dirk's answer is a great one. If you want an appearance like what hist produces, you can also try this:

    buckets <- c(0,1,2,3,4,5,25)
    mydata_hist <- hist(mydata$V3, breaks=buckets, plot=FALSE)
    bp <- barplot(mydata_hist$count, log="y", col="white", names.arg=buckets)
    text(bp, mydata_hist$counts, labels=mydata_hist$counts, pos=1)
    

    The last line is optional, it adds value labels just under the top of each bar. This can be useful for log scale graphs, but can also be omitted.

    I also pass main, xlab, and ylab parameters to provide a plot title, x-axis label, and y-axis label.

    0 讨论(0)
  • 2020-11-27 17:03

    Another option would be to use the package ggplot2.

    ggplot(mydata, aes(x = V3)) + geom_histogram() + scale_x_log10()
    
    0 讨论(0)
提交回复
热议问题