[R] Using logarithmic y-axis (density) in a histogram

Duncan Murdoch murdoch at stats.uwo.ca
Thu Mar 8 14:32:35 CET 2007


On 3/8/2007 8:10 AM, david.bolius at art.admin.ch wrote:
> Hi, 
> 
> I am searching for a possibility to display a logarithimic y-axis in a histogram. With plot that's easy (e.g. 
> plot(1:10, log="y") 
> but for histograms this does not work the same way: I tried
> hist(rnorm(1000), freq=FALSE, seq(-4, 4, .5), ylim=c(0.001, 0.5), log="y") 
> Which gives the expected histogram but also warnings for my log="y" command (""log" is not a graphical parameter in: axis(side, at, labels, tick, line, pos, outer, font, lty, lwd,  ") and no logarithmic y-axis. Any ideas how to achieve that, I couldn't find anything?

I'd say from a graphical perspective it doesn't make sense to use a bar 
chart for a histogram on a log scale (where should the base of the bars 
go?), but if you really want to, you could do it by calling hist() with 
plot=FALSE, and building it yourself using the "histogram" object that 
is returned.  For example,

x <- rnorm(10000)
h <- hist(x, breaks="Scott", plot=FALSE)
plot(h$mids, h$density, log="y", type='b')

Duncan Murdoch



More information about the R-help mailing list