[R] plotting step functions in plot vs. xyplot
deepayan.sarkar at gmail.com
Mon Dec 3 03:01:58 CET 2007
On 12/2/07, Dylan Beaudette <dylan.beaudette at gmail.com> wrote:
> I have noticed an odd inconsistency when plotting a 'step' function
> (type='s') in xyplot() vs. plot().
> For example, given the following data:
> ## generate some profile depths: 0 - 150, in 10 cm increments
> depth <- seq(0,150, by=10)
> ## generate some property: random numbers in this case
> prop <- rnorm(n=length(depth), mean=15, sd=2)
> ## since the 0 is not a depth, and we would like the graph to start from 0
> ## make the first property row (associated with depth 0) the same as the second
> ## property row
> prop <- prop
> ## combine into a table: data read in from a spread sheet would
> already be in this format
> soil <- data.frame(depth=depth, prop=prop)
> ## simple depth plot, as steps: looks good!
> plot(depth ~ prop, data=soil, ylim=c(150,0), type='s', ylab='Depth',
> xlab='Property', main='Property vs. Depth Plot')
> ## now try it with lattice graphics:yuck!
> xyplot(depth ~ prop, data=soil, ylim=c(160,-5), type='s',
> ylab='Depth', xlab='Property', main='Property vs. Depth Plot')
> it looks like the data isn't rotated (?) correctly - i.e. the axis and
> data do not match.
> Now, am I mis-interpreting the meaning of type='s' in lattice graphics?
Yes (though not through any fault of your own). Unile plot(), type='s'
in panel.xyplot() sorts the values before doing the steps. This
difference was not intentional (originally), but it's been around long
enough that I decided to make it a "feature" (i.e., document it) when
I discovered it.
You can still get what you want by sorting on the y-axis, and you can
do that by adding 'horizontal = TRUE'. The more general fix that I had
planned was to have panel.points() etc work as plot, so that you could
xyplot(..., type = "s", panel = panel.points)
It turns out that I haven't implemented that yet, but it should be in
the next update (but you need at least R 2.5.0 to use it).
> here is my R session info:
> R version 2.4.1 (2006-12-18)
> attached base packages:
>  "stats" "graphics" "grDevices" "utils" "datasets" "methods"
>  "base"
> other attached packages:
> could it be that my version of R and lattice is just too old?
> thanks in advance!
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help