[R] log y 'axis' of histogram

Joshua Wiley jwiley.psych at gmail.com
Mon Aug 30 04:39:53 CEST 2010


Hi Derek,

Here is an option using the package ggplot2:

library(ggplot2)
x <- sample(x = 10:50, size = 50, replace = TRUE)
qplot(x = x, geom = "histogram") + scale_y_log()

However, the log scale is often inappropriate for histograms, because
the y-axis represents counts, which could potentially be 0, and
therefore undefined (R outputs -Inf).  Another option using base
graphics would be something along the lines (no pun intended) of:

temp <- hist(x, plot = FALSE) #get histogram data
plot(x = temp$mids, y = log(temp$counts), type = "h")

HTH,

Josh

On Sun, Aug 29, 2010 at 6:58 PM, Derek M Jones <derek at knosof.co.uk> wrote:
> All,
>
> I have been trying to get calls to hist(...) to be plotted
> with the y-axis having a log scale.
>
> I have tried: par(ylog=TRUE)
>
> I have also looked at the histogram package.
>
> Suggestions welcome.
>
> --
> Derek M. Jones                         tel: +44 (0) 1252 520 667
> Knowledge Software Ltd                 mailto:derek at knosof.co.uk
> Source code analysis                   http://www.knosof.co.uk
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list