Draw a logarithmic y axis along the histogram using R

Hi, I am making a histogram with R, but the number of the Y axis is so large that I need to turn it into a logarithmic. See below for my script:

hplot<-read.table("libl") hplot pdf("first_end") hist(hplot$V1, breaks=24, xlim=c(0,250000000), ylim=c(0,2000000),main="first end mapping", xlab="Coordinates") dev.off() 

So how do I change my script? THX

+12
r logarithm histogram
Oct 19 '11 at 21:14
source share
4 answers

You can save the histogram data to configure it before plotting:

 set.seed(12345) x = rnorm(1000) hist.data = hist(x, plot=F) hist.data$counts = log10(hist.data$counts) dev.new(width=4, height=4) hist(x) dev.new(width=4, height=4) plot(hist.data, ylab='log10(Frequency)') 

enter image description here

enter image description here

+17
Oct 19 '11 at
source share

The histogram with the y axis on the logarithm scale will be a rather odd histogram. Technically, it will still fit the definition, but it may look pretty deceiving: the peaks will be flattened relative to the rest of the distribution.

Instead of using log conversion, you thought:

  • Division of counters by 1 million:

    h <- hist(hplot$V1, plot=FALSE)

    h$counts <- h$counts/1e6

    plot(h)

  • Building a histogram as an estimate of density:

    hist(hplot$V1, freq=FALSE)

+4
Oct 20 2018-11-11T00:
source share

Another option is to use plot(density(hplot$V1), log="y") .

This is not a histogram, but it shows approximately the same information, and it avoids the illogical part, where the zero-count bit is not defined in the log space.

Of course, this is relevant only when your data is continuous, and not when it is really categorical or ordered.

+1
Nov 26 '15 at 14:29
source share

You can register your y values ​​for the graph and then add a custom logical y axis.

Here is an example for a table object with random normal distribution numbers:

 # data count = table(round(rnorm(10000)*2)) # plot plot(log(count) ,type="h", yaxt="n", xlab="position", ylab="log(count)") # axis labels yAxis = c(0,1,10,100,1000) # draw axis labels axis(2, at=log(yAxis),labels=yAxis, las=2) 

log data with a log axis label

+1
Feb 13 '16 at 2:02
source share



All Articles