Re: [R] plotting step functions in plot vs. xyplot

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Sun, 2 Dec 2007 18:01:58 -0800

On 12/2/07, Dylan Beaudette <dylan.beaudette_at_gmail.com> wrote:
> Hi,
>
> 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[1] <- prop[2]
> ## 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 do

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).

-Deepayan

>
> here is my R session info:
> sessionInfo()
> R version 2.4.1 (2006-12-18)
> i686-pc-linux-gnu
>
> locale:
> LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
>
> attached base packages:
> [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
> [7] "base"
>
> other attached packages:
> lattice
> "0.14-17"
>
> could it be that my version of R and lattice is just too old?
>
> thanks in advance!
>
> Dylan
>
> ______________________________________________
> 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.
>



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. Received on Mon 03 Dec 2007 - 02:05:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Dec 2007 - 05:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.