Histogram with Logarithmic Scale and custom breaks

后端 未结 7 1875
谎友^
谎友^ 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 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.

提交回复
热议问题