[R] Cumulative lattice histograms
Dear all, It's fairly straightforward to plot cumulative histograms using the hist() function. You do something like: h - hist(rnorm(100), plot=FALSE) h$counts- cumsum(h$counts) plot(h) However, I have failed to find any example where this is done using the lattice histogram() function. I realize I need to slightly alter the panel function panel.histogram. Specifially I would like to add the following line in red, just like I did above: function (x, breaks, equal.widths = TRUE, type = density, nint = round(log2(length(x)) + 1), alpha = plot.polygon$alpha, col = plot.polygon$col, border = plot.polygon$border, lty = plot.polygon$lty, lwd = plot.polygon$lwd, ...) { plot.polygon - trellis.par.get(plot.polygon) xscale - current.panel.limits()$xlim panel.lines(x = xscale[1] + diff(xscale) * c(0.05, 0.95), y = c(0, 0), col = border, lty = lty, lwd = lwd, alpha = alpha) if (length(x) 0) { if (is.null(breaks)) { breaks - if (is.factor(x)) seq_len(1 + nlevels(x)) - 0.5 else if (equal.widths) do.breaks(range(x, finite = TRUE), nint) else quantile(x, 0:nint/nint, na.rm = TRUE) } h - hist.constructor(x, breaks = breaks, ...) h$counts- cumsum(h$counts) y - if (type == count) h$counts else if (type == percent) 100 * h$counts/length(x) else h$intensities breaks - h$breaks nb - length(breaks) if (length(y) != nb - 1) warning(problem with 'hist' computations) if (nb 1) { panel.rect(x = breaks[-nb], y = 0, height = y, width = diff(breaks), col = col, alpha = alpha, border = border, lty = lty, lwd = lwd, just = c(left, bottom)) } } } environment: namespace:lattice My problem is I'm too unexperienced in handling these panel functions to achieve this. Simply copying the panel function, appending the line, and giving it another name obviously doesn't work (it won't find the function hist.constructor). I would very much appreciate help on how this could be done, or some other way to draw cumulative lattice histograms. Thanks in advance, Ola Caster [[alternative HTML version deleted]] __ R-help@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.
Re: [R] histogram() with Date class?
Thanks a lot Deepayan. Could you please inform me what update are you referring to, and give me some very vague sense when it might happen (within weeks, months, or years)? Many thanks Ola 2008/5/8 Deepayan Sarkar [EMAIL PROTECTED]: On 5/8/08, Ola Caster [EMAIL PROTECTED] wrote: Dear help list, Is it possible to draw lattice histograms (i.e. use the histogram() function and not the hist() function) with objects of class Date? Sort of. The default calculation of 'breaks' doesn't work, so histogram(~date, data=my.data) will currently fail (I will fix this in the next update). However, histogram(~date, data=my.data, breaks = 10) histogram(~date, data=my.data, breaks = fd) etc., should work. The help page for histogram is somewhat ambiguous; it says: breaks: [...] Other valid values of 'breaks' are those of the 'breaks' argument in 'hist'. This allows specification of 'breaks' as an integer giving the number of bins (similar to 'nint'), as a character string denoting a method, and as a function. What is meant here is that valid values of breaks in 'hist.default' will work, not those for any other methods. In particular, breaks = months will not work. -Deepayan I've tried solutions like histogram(~date, data=my.data, breaks=months) but it doesn't seem to work. Any suggestions are welcome. Many thanks Ola Caster [[alternative HTML version deleted]] __ R-help@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.
Re: [R] histogram() with Date class?
Sorry for spamming the list... I noticed that if you first produce a date histogram with the hist() function, like this: basic.histogram - hist(my.data$date, breaks = months, plot = FALSE) and then try to transfer the breaks from that histogram to a lattice equivalent, like this: histogram(~date, data=my.data, breaks=basic.histogram$breaks) then I get a histogram that looks exactly like the original one, except the bins are not placed correctly on the x-axis. In my case they were all shifted almost a year to the right. Does anyone know why this happened and if there is a way to deal with it? Many thanks Ola 2008/5/9 Ola Caster [EMAIL PROTECTED]: Thanks a lot Deepayan. Could you please inform me what update are you referring to, and give me some very vague sense when it might happen (within weeks, months, or years)? Many thanks Ola 2008/5/8 Deepayan Sarkar [EMAIL PROTECTED]: On 5/8/08, Ola Caster [EMAIL PROTECTED] wrote: Dear help list, Is it possible to draw lattice histograms (i.e. use the histogram() function and not the hist() function) with objects of class Date? Sort of. The default calculation of 'breaks' doesn't work, so histogram(~date, data=my.data) will currently fail (I will fix this in the next update). However, histogram(~date, data=my.data, breaks = 10) histogram(~date, data=my.data, breaks = fd) etc., should work. The help page for histogram is somewhat ambiguous; it says: breaks: [...] Other valid values of 'breaks' are those of the 'breaks' argument in 'hist'. This allows specification of 'breaks' as an integer giving the number of bins (similar to 'nint'), as a character string denoting a method, and as a function. What is meant here is that valid values of breaks in 'hist.default' will work, not those for any other methods. In particular, breaks = months will not work. -Deepayan I've tried solutions like histogram(~date, data=my.data, breaks=months) but it doesn't seem to work. Any suggestions are welcome. Many thanks Ola Caster [[alternative HTML version deleted]] __ R-help@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.
[R] histogram() with Date class?
Dear help list, Is it possible to draw lattice histograms (i.e. use the histogram() function and not the hist() function) with objects of class Date? I've tried solutions like histogram(~date, data=my.data, breaks=months) but it doesn't seem to work. Any suggestions are welcome. Many thanks Ola Caster [[alternative HTML version deleted]] __ R-help@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.