# [R] lattice: loess smooths based on y-axis values

Raeanne Miller Raeanne.Miller at sams.ac.uk
Fri Jan 18 16:57:04 CET 2013

```Hi Bert,

Thanks for your answer - I've done a good bit of research in to panel functions this afternoon, and they're starting to seem a *little* less intimidating. I didn't know there was a panel.lines, so that's very helpful!

Turns out that panel.loess has an argument 'horizontal', which when set to 'TRUE' bases the smooth on the y axis, as opposed to the x. So by using a panel function:

panel.groups = function(x, y, ..., lty) {
panel.xyplot(x, y, ...)
panel.loess(x,y, horizontal=TRUE, lty=lty,...)
}

in my call to xyplot, it seems I can get the smooth the way I want it, with Depth on the y axis and abundance on the x axis.

Hope that is useful for anyone else interested - and Bert, thanks again for the swift reply!

Raeanne

-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com]
Sent: 18 January 2013 15:08
To: Raeanne Miller
Cc: r-help at r-project.org
Subject: Re: [R] lattice: loess smooths based on y-axis values

Reanne:

UNTESTED:

I would reverse the xyplot call as xyplot(x~y,...) to get all axes set up properly and then just write an explicit panel function using
loess() and predict.loess the "correct" way, e.g. as loess(y~x) to get the (x,y) curve pairs to be plotted  via panel.lines(y,x....). You will have to make sure the pairs are ordered in decreasing x.

This presumes familiarity with loess() and lattice panel functions, of course. Deepayan's book is a comprehensive resource for the latter.
Murrell's also contains a less comprehensive but probably adequate overview. -- If you need anything at all, that is.

-- Bert

On Fri, Jan 18, 2013 at 3:36 AM, Raeanne Miller <Raeanne.Miller at sams.ac.uk> wrote:
> Hi there,
>
> I'm using the lattice package to create an xy plot of abundance vs. depth for 5 stages of barnacle larvae from 5 species. Each panel of the plot represents a different stage, while different loess smoothers within each panel should represent different species.
>
> However, I would like depth to be on the y-axis and abundance to be on the x-axis, because this is more intuitive as an oceanographer. The smoothing curves would then represent the abundance profiles with depth of the larvae. I have tried just switching depth and abundance in the plot formula/coding, but this means that the loess smoother is based on averaging depth values for each abundance, which means that the curves give multiple possible abundances at each depth level. Rather, I would like the loess smooth to still be based on 'averaging' abundance at each depth.
>
> Essentially, I would like a mirror image of the plot from this code:
>
> ord<-order(Zoo_nocyp\$Depth_m, decreasing=TRUE)
> xyplot(log10Ab[ord]~factor(-1*Depth_m[ord])|StageF[ord],
>          data=Zoo_nocyp[Zoo_nocyp\$log10Ab!="0" ,],
>          type=c("p","smooth"),
>          par.settings=list(strip.background=list(col="white"), fontzize=list(text=16,points=10), lty = 0),
>          space=0, border=NA,
>          col=c("blue","red","green","purple","orange"),
>          stack=TRUE, groups=Zoo_nocyp\$SpeciesF,
>          key=
>            list(title="Species", cex.title=1,text=list(c("BB","BC","CH","SB","VS")),space="right",
>                 rectangles=list(size=2, border="white",col=c("blue","red","green","purple","orange"))),
>          xlab="depth (m)", ylab="log10 abundance",
>          layout=c(2,3))
>
> which is different to what you get when you switch log10Ab and Depth_m.
>
> Is there any way to specify the smoothers to be based on sequential y-axis values, rather than x? Or to get the mirror image of a plot?
>
> I can provide a dataset offline, for those interested.
>
> Thanks very much,
>
> Raeanne
>
>
> The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered charity (9206). SAMS has an actively trading wholly owned subsidiary company: SAMS Research Services Ltd a Limited Company (SC224404). All Companies in the group are registered in Scotland and share a registered office at Scottish Marine Institute, Oban Argyll PA37 1QA. The content of this message may contain personal views which are not the views of SAMS unless specifically stated. Please note that all email traffic is monitored for purposes of security and spam filtering. As such individual emails may be examined in more detail.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website: