Dear All, I would like to be able to plot histograms/densities on a semi-log or log-log scale. I found several suggestions online http://tolstoy.newcastle.edu.au/R/help/05/09/12044.html https://stat.ethz.ch/pipermail/r-help/2002-June/022295.html http://www.harding.edu/fmccown/R/#histograms Now, consider the code snippet taken from http://www.harding.edu/fmccown/R/#histograms # Get a random log-normal distribution r <- rlnorm(1000) # Get the distribution without plotting it using tighter breaks h <- hist(r, plot=F, breaks=c(seq(0,max(r)+1, .1))) # Plot the distribution using log scale on both axes, and use # blue points plot(h$counts, log="xy", pch=20, col="blue", main="Log-normal distribution", xlab="Value", ylab="Frequency") This is very close to what I need, but how can I have filled rectangles in the plot, so that it looks more like a traditional histogram? Kind Regards Lorenzo
> I would like to be able to plot histograms/densities on a semi-log or > log-log scale.> # Get a random log-normal distribution > r <- rlnorm(1000) > > # Get the distribution without plotting it using tighter breaks > h <- hist(r, plot=F, breaks=c(seq(0,max(r)+1, .1))) > > # Plot the distribution using log scale on both axes, and use > # blue points > plot(h$counts, log="xy", pch=20, col="blue", > main="Log-normal distribution", > xlab="Value", ylab="Frequency") > > This is very close to what I need, but how can I have filled rectangles > in the plot, so that it looks more like a traditional histogram?You can use type="h" to specify histogram-like plotting. You probably also want to use a linear y-scale to make frequencies easier to compare. Change the last line to: plot(h$mids, h$counts, log="x", pch=20, col="blue", main="Log-normal distribution", xlab="Value", ylab="Frequency", type="h") Alternatively, plot a histogram of the log data, and draw your own axes: logr <- log(r) par(las=2) h2 <- hist(logr, axes=FALSE, breaks=seq(min(logr)-1, max(logr)+1, .5)) Axis(side=2) Axis(side=1, at=h2$mids, labels=format(exp(h2$mids), digits=1), lty=NULL) Or, even better, install the ggplot2 package and try something like: qplot(r, geom="histogram") + scale_x_continuous(trans="log10") Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:22}}
How about using the ggplot2 package? Does this give you something along the lines of what you want? ======================================================================library(ggplot2) r <- data.frame(rlnorm(1000)) names(r) <- "rr" ggplot(r, aes(rr)) + geom_histogram()+ scale_x_log10() + scale_y_log10() ======================================================================== --- On Mon, 7/20/09, Lorenzo Isella <lorenzo.isella at gmail.com> wrote:> From: Lorenzo Isella <lorenzo.isella at gmail.com> > Subject: [R] Histograms on a log scale > To: r-help at r-project.org > Received: Monday, July 20, 2009, 5:17 AM > Dear All, > I would like to be able to plot histograms/densities on a > semi-log or log-log scale. > I found several suggestions online > > http://tolstoy.newcastle.edu.au/R/help/05/09/12044.html > https://stat.ethz.ch/pipermail/r-help/2002-June/022295.html > http://www.harding.edu/fmccown/R/#histograms > > Now, consider the code snippet taken from http://www.harding.edu/fmccown/R/#histograms > > # Get a random log-normal distribution > r <- rlnorm(1000) > > # Get the distribution without plotting it using tighter > breaks > h <- hist(r, plot=F, breaks=c(seq(0,max(r)+1, .1))) > > # Plot the distribution using log scale on both axes, and > use > # blue points > plot(h$counts, log="xy", pch=20, col="blue", > ???main="Log-normal distribution", > ???xlab="Value", ylab="Frequency") > > This is very close to what I need, but how can I have > filled rectangles in the plot, so that it looks more like a > traditional histogram? > Kind Regards > > Lorenzo > > ______________________________________________ > 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. >__________________________________________________________________ The new Internet Explorer? 8 - Faster, safer, easier. Optimized for Yahoo!
John Kane wrote:> How about using the ggplot2 package? Does this give you something along the lines of what you want? > ======================================================================> library(ggplot2) > r <- data.frame(rlnorm(1000)) > names(r) <- "rr" > > ggplot(r, aes(rr)) + geom_histogram()+ scale_x_log10() + scale_y_log10() > > ========================================================================> > --- On Mon, 7/20/09, Lorenzo Isella <lorenzo.isella at gmail.com> wrote: > > >> From: Lorenzo Isella <lorenzo.isella at gmail.com> >> Subject: [R] Histograms on a log scale >> To: r-help at r-project.org >> Received: Monday, July 20, 2009, 5:17 AM >> Dear All, >> I would like to be able to plot histograms/densities on a >> semi-log or log-log scale. >> I found several suggestions online >> >> http://tolstoy.newcastle.edu.au/R/help/05/09/12044.html >> https://stat.ethz.ch/pipermail/r-help/2002-June/022295.html >> http://www.harding.edu/fmccown/R/#histograms >> >> Now, consider the code snippet taken from http://www.harding.edu/fmccown/R/#histograms >> >> # Get a random log-normal distribution >> r <- rlnorm(1000) >> >> # Get the distribution without plotting it using tighter >> breaks >> h <- hist(r, plot=F, breaks=c(seq(0,max(r)+1, .1))) >> >> # Plot the distribution using log scale on both axes, and >> use >> # blue points >> plot(h$counts, log="xy", pch=20, col="blue", >> main="Log-normal distribution", >> xlab="Value", ylab="Frequency") >> >> This is very close to what I need, but how can I have >> filled rectangles in the plot, so that it looks more like a >> traditional histogram? >> Kind Regards >> >> Lorenzo >> >> ______________________________________________ >> 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. >> >> > > > __________________________________________________________________ > The new Internet Explorer? 8 - Faster, safer, easier. Optimized for Yahoo! Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/ >Hello, And thanks for answering. Well, I think I found what I wanted (maybe in a cumbersome way). ggplot2 can probably do the same and a lot more, but perhaps it is like using a cannon to kill a bird. See the code snippet at the end of the email. Kind Regards Lorenzo rm(list=ls()) set.seed(1234) my_seq <- rlnorm(1000) log_binning <- function(x_min,x_max,n_breaks=10){ delta_log <- log(x_max/x_min)/(n_breaks-1) my_seq<-seq(0,n_breaks-1) log_breaks <- x_min*exp(my_seq*delta_log) } freq <- table(my_seq) n_bins <- 20 my_breaks2 <- log_binning(min(my_seq), max(my_seq), n_bins) #this creates a log-spaced bin structure h2 <- hist(my_seq, plot=F, my_breaks2) #this creates a histogram but does not plot it. pdf("histogram_lognormal.pdf") par( mar = c(4.5,5, 2, 1) + 0.1) i <- seq(1,(length(h2$breaks)-1)) #this counts the rectangles I am about to generate plot(h2$mids, h2$counts, col="blue",ylab="Frequency", log="x", ,cex.axis=1.4,cex.lab=1.6, xlab="Value of Random Number", main="") rect(h2$breaks[i], 0, h2$breaks[i+1],h2$count )#this generates a set of rectangles which are useful to visualize the underlying #bin structure dev.off()