Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make y-axis logarithmic in histogram using R [duplicate]

Hi I'm making histogram using R, but the number of Y axis is so large that I need to turn it into logarithmic.See below 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 should I change my script? thx

like image 644
LookIntoEast Avatar asked Oct 19 '11 21:10

LookIntoEast


People also ask

How do you extend the Y-axis on a histogram in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change the axis titles in a histogram in R?

You can change the title of the histogram by adding main as an argument to hist() function. In this case, you make a histogram of the AirPassengers data set with the title “Histogram for Air Passengers”: If you want to adjust the label of the x-axis, add xlab .


3 Answers

You can save the histogram data to tweak 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

like image 181
John Colby Avatar answered Dec 20 '22 14:12

John Colby


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

It's not a histogram, but it shows just about the same information, and it avoids the illogical part where a bin with zero counts is not well-defined in log-space.

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

like image 32
pipefish Avatar answered Dec 20 '22 14:12

pipefish


A histogram with the y-axis on the log scale will be a rather odd histogram. Technically it will still fit the definition, but it could look rather misleading: the peaks will be flattened relative to the rest of the distribution.

Instead of using a log transformation, have you considered:

  • Dividing the counts by 1 million:

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

    h$counts <- h$counts/1e6

    plot(h)

  • Plotting the histogram as a density estimate:

    hist(hplot$V1, freq=FALSE)

like image 34
Hong Ooi Avatar answered Dec 20 '22 14:12

Hong Ooi