david.bolius at art.admin.ch
2007-Mar-08 13:10 UTC
[R] Using logarithmic y-axis (density) in a histogram
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? Best regards David *************************************************************** Dr. David Bolius Agroscope Reckenholz-T?nikon ART Gruppe Lufthygiene/Klima Reckenholzstrasse 191, CH-8046 Z?rich Tel. ++41 (0)44 / 377 75 13 FAX ++41 (0)44 / 377 72 01 david.bolius at art.admin.ch http://www.art.admin.ch/
Duncan Murdoch
2007-Mar-08 13:32 UTC
[R] Using logarithmic y-axis (density) in a histogram
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
You might also want to try "density", since it can theoretically have non-zero bins, since it doesn't use "bins". For example, take a Weibull distribution, which could look better with a log y-axis: x <- rweibull(1000,1,5) par(mfrow=c(2,1)) plot(density(x,from=0)) rug(x) plot(density(x,from=0),log="y") rug(x) you may need to fiddle with the "bw" (bandwidth) parameter of "density", since this controls the smoothness of the kernel (see ?density). +mt