[R] plotting step functions in plot vs. xyplot

Deepayan Sarkar 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:
> 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.
>



More information about the R-help mailing list