Quantcast

Drawing a line in xyplot

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Drawing a line in xyplot

wcheckle
This post was updated on .
i am trying to replicate the following graph using xyplot :

attach(x)
plot ( jitter(type), mortality, pch=16,  xlim = c(0.25, 3.75))
lines ( c(1-0.375,1.375) , c ( median(mortality[type==1]), median(mortality[type==1])), lwd=5,col=2)
lines ( c(2-0.375,2.375) , c ( median(mortality[type==2]), median(mortality[type==2])), lwd=5,col=2)
lines ( c(3-0.375,3.375) , c ( median(mortality[type==3]), median(mortality[type==3])), lwd=5,col=2)
detach(x)

in the above graph, i draw a median line for "mortality" (range from 5 to 35) by "type" (1,2 or 3).  i now have an additional variable "attend" (0 or 1). within each panel (three panels, one for each "type"), i would like to draw the median "mortality" for each instance of "attend".  i have been able to get as far as plotting everything but the median lines:

x11(height=8,width=11)
xyplot ( mortality ~ attend|type,
panel=function(x,y,subscripts){panel.grid(lty=5); panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)},
strip=strip.custom(which.given=1, bg="orange"),data =x,aspect=2:1,layout=c(3,1))

any suggestions on how to add the median lines of "mortality" for each instance of "attend" within each panel of "type"?

thank you

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

David Winsemius

On Apr 6, 2012, at 9:41 PM, wcheckle wrote:

> i am trying to replicate the following graph using xyplot :
>
> attach(x)
> plot ( jitter(type), mortality, pch=16,  xlim = c(0.25, 3.75))
> lines ( c(1-0.375,1.375) , c ( median(mortality[type==1]),
> median(mortality[type==1])), lwd=5,col=2)
> lines ( c(2-0.375,2.375) , c ( median(mortality[type==2]),
> median(mortality[type==2])), lwd=5,col=2)
> lines ( c(3-0.375,3.375) , c ( median(mortality[type==3]),
> median(mortality[type==3])), lwd=5,col=2)
> detach(x)
>
> in the above graph, i draw a median line for "mortality" (range from  
> 5 to
> 35) by "type" (1,2 or 3).  i now have an additional variable  
> "attend" (0 or
> 1). within each panel (three panel, one for each "type"), i would  
> like to
> draw the median "mortality" for each instance of "attend".  i have  
> been able
> to get as far as plotting everything but the median lines:
>
> x11(height=8,width=11)
> xyplot ( mortality ~ attend|type,
> panel=function(x,y,subscripts){panel.grid(lty=5);
> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)},
> strip=strip.custom(which.given=1, bg="orange"),data
> =x,aspect=2:1,layout=c(3,1))
>
> any suggestions on how to add the median lines of "mortality" for each
> instance of "attend" within each panel of "type"?

Without data, .... I will only offer that there are functions to draw  
lines inside lattice panels. (And will not that this information is  
also offered on the main ?Lattice page.)

??llines
??panel.abline

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

wcheckle
In reply to this post by wcheckle
here is the data (fyi this is made-up data)

x = as.data.frame ( cbind(c(5,8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20), c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3),c(1,0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0)))
names(x)=c("mortality","type","attend")

here is the image:


revised code with image:

x11(height=8,width=11)
xyplot ( mortality ~ factor(attend)|type,
panel=function(x,y){panel.grid(lty=5); panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)},
strip=strip.custom(which.given=1, bg="orange"),data =x,aspect=2:1,layout=c(3,1))



i am trying to replicate the "red" mean lines on the xyplot graph

abline won't do it.  llines may be able to do it, but i don't know how to use/implement

thanks

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

David Winsemius

On Apr 7, 2012, at 11:35 AM, wcheckle wrote:

> here is the data (fyi this is made-up data)
>
> x = as.data.frame (
> cbind
> (c
> (5,8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20
> ),
> c
> (1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3
> ),c(1,0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0)))
> names(x)=c("mortality","type","attend")

Sidebar not related to the question at hand: I would like to get hands  
on the person who is teaching the malpractice of using the form  
as.data.frame(cbind(vectors)))  and ....what would be sufficiently  
motivational?  ... strangulation would be a bit severe, but perhaps  
carefully applied thumb pressure to the cricoid cartilage for  
sufficient interval to gain attention? I have seen several instances  
of that (mal)form in various rhelp-ish venues in the last couple of  
weeks and it is a common source of obscure error.

xdat = data.frame ( mortality =c(5,  
8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20
), type=
c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3),  
attend c(1, 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0))

The reason for NOT using cbind is that it forces all vectors to have  
the same mode.  The whole point of having data.frames is to allow you  
to mix modes. So that constriction is completely boneheaded.

And please learn to use spaces.

>
> here is the image:
> http://r.789695.n4.nabble.com/file/n4539596/x.jpg
>
> revised code with image:
>
> x11(height=8,width=11)
> xyplot ( mortality ~ factor(attend)|type,
> panel=function(x,y){panel.grid(lty=5);
> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)},
> strip=strip.custom(which.given=1, bg="orange"),data
> =x,aspect=2:1,layout=c(3,1))
>
> http://r.789695.n4.nabble.com/file/n4539596/x1.jpg
>
> i am trying to replicate the "red" mean lines on the xyplot graph
>
> abline won't do it.  llines may be able to do it, but i don't know  
> how to
> use/implement
>

I didn't say to 'abline'. I said 'panel.abline' and the help page for  
panel.abline also include 'panel.segments' which it becomes clear you  
wanted ...  now that we have data and can see what you were looking at.

xyplot(mortality ~ type, data=xdat,
                panel=function(x,y){
                    panel.xyplot(x,y, jitter.x=TRUE)
                    panel.segments(x0=c(.9, 1.9, 2.9),
                                    x1=c(1.1,2.1,3.1),
                                    y0=tapply(xdat$mortality, xdat
$type, median),
                                    y1=tapply(xdat$mortality, xdat
$type, median),
                              col="red", lwd=3 )
                        })


> thanks
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Drawing-a-line-in-xyplot-tp4538689p4539596.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

ilai-2
On Sat, Apr 7, 2012 at 11:16 AM, David Winsemius <[hidden email]> wrote:

> xyplot(mortality ~ type, data=xdat,
>               panel=function(x,y){
>                   panel.xyplot(x,y, jitter.x=TRUE)
>                   panel.segments(x0=c(.9, 1.9, 2.9),
>                                   x1=c(1.1,2.1,3.1),
>                                   y0=tapply(xdat$mortality, xdat$type,
> median),
>                                   y1=tapply(xdat$mortality, xdat$type,
> median),
>                             col="red", lwd=3 )
>
>                       })
>

Actually the OP had formula = mortality ~ factor(attend)|type,
(two conditioning factors). This approach will work but will require
1)  replace type with attend in tapply
2) subset conditional on packet.number
3) loop over the segments for the two sets of x-coords

An alternative will be

bwplot(mortality ~ factor(attend)|type,data=xdat,
 pch=95,cex=5,col=2,
 par.settings=list(
  box.rectangle = list(col = 'transparent'),
  box.umbrella = list(col = 'transparent')
 ),
 panel=function(x,y,...){
  panel.grid(lty=5)
  panel.bwplot(x,y,...)
  panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
 }
)

Hope this helps


>
>> thanks
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Drawing-a-line-in-xyplot-tp4538689p4539596.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
> David Winsemius, MD
> West Hartford, CT
>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

wcheckle
In reply to this post by wcheckle
Thank you David, the bwplot option does what I need:

x11(height=8,width=11)
bwplot ( mortality~ attend|type,
 pch=95,cex=5,col=2,
 par.settings=list(
  box.rectangle = list(col = "transparent"),
  box.umbrella = list(col = "transparent"),
  plot.symbol = list(col = "transparent")
 ),
 panel=function(x,y,...){
  panel.grid(lty=5)
  panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
  panel.bwplot(x,y,...)
 },
data = x,aspect=2:1,layout=c(3,1))


However, I am interested in also learning how to do it in xyplot as well.  I wasn’t able to follow the last two set of instructions (condition on packet.number and loop over segments), wondering if I can ask for your help for the correct code (my attempt resulted in all three mean lines within each panel):

x11(height=8,width=11)
xyplot ( mortality ~ attend|type,
panel=function(x,y)
{
panel.grid(lty=5)
panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
for(i in 1:3)
{
panel.segments(x0=c(0.7, 1.7),
x1=c(1.3, 2.3),
y0=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),
y1=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),col="red",lwd=4)
}
},
data = x,aspect=2:1,layout=c(3,1))

thank you again. I also found your info on data.frame useful.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

David Winsemius

On Apr 7, 2012, at 10:29 PM, wcheckle wrote:

> Thank you David, the bwplot option does what I need:
>
> x11(height=8,width=11)
> bwplot ( mortality~ attend|type,
> pch=95,cex=5,col=2,
> par.settings=list(
>  box.rectangle = list(col = "transparent"),
>  box.umbrella = list(col = "transparent"),
>  plot.symbol = list(col = "transparent")
> ),
> panel=function(x,y,...){
>  panel.grid(lty=5)
>  panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>  panel.bwplot(x,y,...)
> },
> data = x,aspect=2:1,layout=c(3,1))
>
>
> However, I am interested in also learning how to do it in xyplot as  
> well.  I
> wasn’t able to follow the last two set of instructions (condition on
> packet.number and loop over segments), wondering if I can ask for  
> your help
> for the correct code (my attempt resulted in all three mean lines  
> within
> each panel):
>
> x11(height=8,width=11)
> xyplot ( mortality ~ attend|type,
> panel=function(x,y)
> {
> panel.grid(lty=5)
> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
> for(i in 1:3)
> {
> panel.segments(x0=c(0.7, 1.7),
> x1=c(1.3, 2.3),
> y0=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),
> y1=c(tapply(x$mortality[x$type==i], x$attend[x$type==i],
> median)),col="red",lwd=4)
> }
> },
> data = x,aspect=2:1,layout=c(3,1))
>
> thank you again. I also found your info on data.frame useful.

  I haven't figured out how to do it with the interaction of 'attend'  
and ''type' from inside xyplot. I'm thinking I might be able to  
succeed using trellis.focus() to address separate "columns" within a  
particular panel.

This will draw  segments at (1,20) and (2,30) without resorting to low  
level grid/viewport stuff.

  trellis.unfocus(); trellis.focus("panel", 1, 1)
  do.call("panel.segments", list(x0=1,y0=20,x1=1.2, y1=20))
  trellis.unfocus()
  trellis.unfocus(); trellis.focus("panel", 1, 1)
  do.call("panel.segments", list(x0=2,y0=30,x1=2.2, y1=30))
  trellis.unfocus()

--
David


David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

ilai-2
In reply to this post by wcheckle
On Sat, Apr 7, 2012 at 8:29 PM, wcheckle <[hidden email]> wrote:

> Thank you David, the bwplot option does what I need:
<snip>
> However, I am interested in also learning how to do it in xyplot as well.  I
> wasn’t able to follow the last two set of instructions

That was me. Sorry for any confusion. wcheckle, these were not
instructions but ramblings on an earlier code which used the original
data.frame inside the lattice panel function, which in my view is an
added complication in the case of only 2-3 conditioning variables. You
were not meant to try and follow (the alternative was given in the
form of bwplot).
Your original post had two plots - in base graphics you had one
conditional variable (type) and median lines, and a second was lattice
with 2 variables (attend|type) without median lines. You were offered
2 solutions - David's to reproduce the first plot in lattice, and the
bwplot to add medians to the second.
You could work through the examples in lattice and on-line to find
there is a multitude of ways to add features to grid/lattice plots. An
example with panel.segments might look (untested) something like:

panel=function(x,y,...)
{
panel.xyplot(x,y,...)
yy<- tapply(y,x,median)
panel.segments(x0, x1, y0= yy , y1= yy , col="red",lwd=4)
}

Hope that clarifies things, and again sorry everyone for any confusion
that may have resulted.

Best,
Elai

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

John Maindonald
In reply to this post by wcheckle
This can be simplified by using the layering abilities that Felix Andrews
made available in latticeExtra.  These are too little known.  These pretty
much make it unnecessary to resort to trellis.focus(), at least in such
cases as this.  These layering abilities are too little known:

library(latticeExtra)
x11(height=8,width=11)
xdat = data.frame(mortality =c(5, 8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20), type=
c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3), attend = c(1, 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0))
gph <- xyplot ( mortality ~ attend|type,
panel=function(x,y)
{
panel.grid(lty=5)
panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
for(i in 1:3)
{
panel.segments(x0=c(0.7, 1.7),
x1=c(1.3, 2.3),
y0=with(xdat, c(tapply(mortality[type==i], attend[type==i], median))),
y1=with(xdat, c(tapply(mortality[type==i], attend[type==i],
median))),col="red",lwd=4)
}
},
data = xdat, aspect=2:1,layout=c(3,1))

## Now create a layer object that will add the further segments.
addlayer <- layer(panel.segments(x0=1,y0=20,x1=1.2, y1=20),
                panel.segments(x0=2,y0=30,x1=2.2, y1=30))

gph+addlayer

The code that produces the object gph would also be simpler
and easier to follow if some relevant part was separated out into
a separate layer.

See also my notices on layering of lattice objects at:
http://www.maths.anu.edu.au/%7Ejohnm/r-book/add-graphics.html

John Maindonald             email: [hidden email]
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Mathematics & Its Applications, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
http://www.maths.anu.edu.au/~johnm

On 08/04/2012, at 8:00 PM, [hidden email] wrote:

> From: David Winsemius <[hidden email]>
> Subject: Re: [R] Drawing a line in xyplot
> Date: 8 April 2012 2:17:22 PM AEST
> To: wcheckle <[hidden email]>
> Cc: [hidden email]
>
>
>
> On Apr 7, 2012, at 10:29 PM, wcheckle wrote:
>
>> Thank you David, the bwplot option does what I need:
>>
>> x11(height=8,width=11)
>> bwplot ( mortality~ attend|type,
>> pch=95,cex=5,col=2,
>> par.settings=list(
>> box.rectangle = list(col = "transparent"),
>> box.umbrella = list(col = "transparent"),
>> plot.symbol = list(col = "transparent")
>> ),
>> panel=function(x,y,...){
>> panel.grid(lty=5)
>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>> panel.bwplot(x,y,...)
>> },
>> data = x,aspect=2:1,layout=c(3,1))
>>
>>
>> However, I am interested in also learning how to do it in xyplot as well.  I
>> wasn’t able to follow the last two set of instructions (condition on
>> packet.number and loop over segments), wondering if I can ask for your help
>> for the correct code (my attempt resulted in all three mean lines within
>> each panel):
>>
>> x11(height=8,width=11)
>> xyplot ( mortality ~ attend|type,
>> panel=function(x,y)
>> {
>> panel.grid(lty=5)
>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>> for(i in 1:3)
>> {
>> panel.segments(x0=c(0.7, 1.7),
>> x1=c(1.3, 2.3),
>> y0=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),
>> y1=c(tapply(x$mortality[x$type==i], x$attend[x$type==i],
>> median)),col="red",lwd=4)
>> }
>> },
>> data = x,aspect=2:1,layout=c(3,1))
>>
>> thank you again. I also found your info on data.frame useful.
>
> I haven't figured out how to do it with the interaction of 'attend' and ''type' from inside xyplot. I'm thinking I might be able to succeed using trellis.focus() to address separate "columns" within a particular panel.
>
> This will draw  segments at (1,20) and (2,30) without resorting to low level grid/viewport stuff.
>
> trellis.unfocus(); trellis.focus("panel", 1, 1)
> do.call("panel.segments", list(x0=1,y0=20,x1=1.2, y1=20))
> trellis.unfocus()
> trellis.unfocus(); trellis.focus("panel", 1, 1)
> do.call("panel.segments", list(x0=2,y0=30,x1=2.2, y1=30))
> trellis.unfocus()
>
> --
> David
>
>
> David Winsemius, MD
> West Hartford, CT

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

David Winsemius

On Apr 8, 2012, at 7:24 PM, John Maindonald wrote:

> This can be simplified by using the layering abilities that Felix  
> Andrews
> made available in latticeExtra.  These are too little known.  These  
> pretty
> much make it unnecessary to resort to trellis.focus(), at least in  
> such
> cases as this.  These layering abilities are too little known:
>
> library(latticeExtra)
> x11(height=8,width=11)
> xdat = data.frame(mortality =c(5,  
> 8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20
> ), type=
> c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3),  
> attend = c(1,  
> 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0))
> gph <- xyplot ( mortality ~ attend|type,
> panel=function(x,y)
> {
> panel.grid(lty=5)
> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
> for(i in 1:3)
> {
> panel.segments(x0=c(0.7, 1.7),
> x1=c(1.3, 2.3),
> y0=with(xdat, c(tapply(mortality[type==i], attend[type==i], median))),
> y1=with(xdat, c(tapply(mortality[type==i], attend[type==i],
> median))),col="red",lwd=4)
> }
> },
> data = xdat, aspect=2:1,layout=c(3,1))
>
> ## Now create a layer object that will add the further segments.
> addlayer <- layer(panel.segments(x0=1,y0=20,x1=1.2, y1=20),
>                 panel.segments(x0=2,y0=30,x1=2.2, y1=30))
Thanks for that John, but I think you missed my point. The lines I was  
adding were supposed to only be an example of how to put in single  
median value. The task with which I felt I had failed was to plot six  
separate medians,  .... two different ones in each panel. I was  
basically throwing up my hands in dealing with the formula interface  
and suggesting going to a "hand-crafted approach. I assumed that  
wcheckle would drop the panel.segments call and individually stick in  
the median values, perhaps using a for-loop.

--
David

>
> gph+addlayer
>
> The code that produces the object gph would also be simpler
> and easier to follow if some relevant part was separated out into
> a separate layer.
>
> See also my notices on layering of lattice objects at:
> http://www.maths.anu.edu.au/%7Ejohnm/r-book/add-graphics.html
>
> John Maindonald             email: [hidden email]
> phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
> Centre for Mathematics & Its Applications, Room 1194,
> John Dedman Mathematical Sciences Building (Building 27)
> Australian National University, Canberra ACT 0200.
> http://www.maths.anu.edu.au/~johnm
>
> On 08/04/2012, at 8:00 PM, [hidden email] wrote:
>
>> From: David Winsemius <[hidden email]>
>> Subject: Re: [R] Drawing a line in xyplot
>> Date: 8 April 2012 2:17:22 PM AEST
>> To: wcheckle <[hidden email]>
>> Cc: [hidden email]
>>
>>
>>
>> On Apr 7, 2012, at 10:29 PM, wcheckle wrote:
>>
>>> Thank you David, the bwplot option does what I need:
>>>
>>> x11(height=8,width=11)
>>> bwplot ( mortality~ attend|type,
>>> pch=95,cex=5,col=2,
>>> par.settings=list(
>>> box.rectangle = list(col = "transparent"),
>>> box.umbrella = list(col = "transparent"),
>>> plot.symbol = list(col = "transparent")
>>> ),
>>> panel=function(x,y,...){
>>> panel.grid(lty=5)
>>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>>> panel.bwplot(x,y,...)
>>> },
>>> data = x,aspect=2:1,layout=c(3,1))
>>>
>>>
>>> However, I am interested in also learning how to do it in xyplot  
>>> as well.  I
>>> wasn’t able to follow the last two set of instructions (condition on
>>> packet.number and loop over segments), wondering if I can ask for  
>>> your help
>>> for the correct code (my attempt resulted in all three mean lines  
>>> within
>>> each panel):
>>>
>>> x11(height=8,width=11)
>>> xyplot ( mortality ~ attend|type,
>>> panel=function(x,y)
>>> {
>>> panel.grid(lty=5)
>>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>>> for(i in 1:3)
>>> {
>>> panel.segments(x0=c(0.7, 1.7),
>>> x1=c(1.3, 2.3),
>>> y0=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),
>>> y1=c(tapply(x$mortality[x$type==i], x$attend[x$type==i],
>>> median)),col="red",lwd=4)
>>> }
>>> },
>>> data = x,aspect=2:1,layout=c(3,1))
>>>
>>> thank you again. I also found your info on data.frame useful.
>>
>> I haven't figured out how to do it with the interaction of 'attend'  
>> and ''type' from inside xyplot. I'm thinking I might be able to  
>> succeed using trellis.focus() to address separate "columns" within  
>> a particular panel.
>>
>> This will draw  segments at (1,20) and (2,30) without resorting to  
>> low level grid/viewport stuff.
>>
>> trellis.unfocus(); trellis.focus("panel", 1, 1)
>> do.call("panel.segments", list(x0=1,y0=20,x1=1.2, y1=20))
>> trellis.unfocus()
>> trellis.unfocus(); trellis.focus("panel", 1, 1)
>> do.call("panel.segments", list(x0=2,y0=30,x1=2.2, y1=30))
>> trellis.unfocus()
>>
>> --
>> David
>>
>>
>> David Winsemius, MD
>> West Hartford, CT
>
David Winsemius, MD
West Hartford, CT


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

wcheckle
In reply to this post by wcheckle
i appreciate all the interest in my question, and thank you Elai for both of your suggestions, which work very well.
Elai, your last code was particularly simple and helpful to generate the figure i was looking for.

x11(height=8,width=11)
par(lend=2)
xdat = data.frame(mortality =c(5, 8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20), type=
c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3), attend = c(1, 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0))
xyplot ( mortality ~ factor(attend)|type,
panel=function(x,y)
{
panel.grid(lty=5)
panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
yy<- tapply(y,x,median)
panel.segments(x0=c(0.7,1.7), x1=c(1.3,2.3), y0= yy , y1= yy , col="red",lwd=5)
},
data = xdat, aspect=2:1,layout=c(3,1))



as a side note, the par(lend=2) function did not square the ends of the lines for panel.segments in xyplot (i was trying to get square ends instead of round ends).

John: thank you for your information regarding latticeExtra and the layer function, which will be helpful for future figures.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Drawing a line in xyplot

John Maindonald
In reply to this post by wcheckle
PS: The following shows possibilities that are available using latticeExtra layering:

## Best make type and attend factors
xdat = data.frame(mortality =c(5, 8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20), type=
factor(c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3)),
attend = factor(c(1, 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0)))


gph <- xyplot (mortality ~ attend|type, pch=16,
                         data = xdat, aspect=2:1,layout=c(3,1))

one4all <- layer(avy <- median(xdat$mortality),
      panel.segments(0.1, avy, 0.3, avy, col="red",lwd=4),
      panel.segments(0.7, avy, 1, avy, col="red",lwd=4))

medbytype <- layer(avy <- median(y),
      panel.segments(0.1, avy, 0.3, avy, col="red",lwd=4),
      panel.segments(0.7, avy, 1, avy, col="red",lwd=4))

interact <- layer(panel.average(x, y, fun=median, col='red', lwd=4))

Compare

gph + one4all
(shows overall median lines in all 3 panels)

gph + medbytype
(shows separate median lines for the separate panels)

gph+interact
(gives a form of interaction plot)

NB x (if its values are used) and y are local to the individual panel

NB also that layer() accepts a data argument.  The following is an
alternative way to calculate one4all:

one4all <- layer(data=xdat, avy <- median(mortality),
      panel.segments(0.1, avy, 0.3, avy, col="red",lwd=4),
      panel.segments(0.7, avy, 1, avy, col="red", lwd=4))

John Maindonald.


> This can be simplified by using the layering abilities that Felix Andrews
> made available in latticeExtra.  These are too little known.  These pretty
> much make it unnecessary to resort to trellis.focus(), at least in such
> cases as this.  These layering abilities are too little known:
>
> library(latticeExtra)
> x11(height=8,width=11)
> xdat = data.frame(mortality =c(5, 8,7,5,8,10,11,6,4,5,20,25,27,30,35,32,28,21,20,34,11,15,18,12,15,12,10,15,19,20), type=
> c(1, 1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3), attend = c(1, 0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,1,1,0,1,0,1,0,0,0))
> gph <- xyplot ( mortality ~ attend|type,
> panel=function(x,y)
> {
> panel.grid(lty=5)
> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
> for(i in 1:3)
> {
> panel.segments(x0=c(0.7, 1.7),
> x1=c(1.3, 2.3),
> y0=with(xdat, c(tapply(mortality[type==i], attend[type==i], median))),
> y1=with(xdat, c(tapply(mortality[type==i], attend[type==i],
> median))),col="red",lwd=4)
> }
> },
> data = xdat, aspect=2:1,layout=c(3,1))
>
> ## Now create a layer object that will add the further segments.
> addlayer <- layer(panel.segments(x0=1,y0=20,x1=1.2, y1=20),
>                 panel.segments(x0=2,y0=30,x1=2.2, y1=30))
>
> gph+addlayer
>
> The code that produces the object gph would also be simpler
> and easier to follow if some relevant part was separated out into
> a separate layer.
>
> See also my notices on layering of lattice objects at:
> http://www.maths.anu.edu.au/%7Ejohnm/r-book/add-graphics.html
>
> John Maindonald             email: [hidden email]
> phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
> Centre for Mathematics & Its Applications, Room 1194,
> John Dedman Mathematical Sciences Building (Building 27)
> Australian National University, Canberra ACT 0200.
> http://www.maths.anu.edu.au/~johnm
>
> On 08/04/2012, at 8:00 PM, [hidden email] wrote:
>
>> From: David Winsemius <[hidden email]>
>> Subject: Re: [R] Drawing a line in xyplot
>> Date: 8 April 2012 2:17:22 PM AEST
>> To: wcheckle <[hidden email]>
>> Cc: [hidden email]
>>
>>
>>
>> On Apr 7, 2012, at 10:29 PM, wcheckle wrote:
>>
>>> Thank you David, the bwplot option does what I need:
>>>
>>> x11(height=8,width=11)
>>> bwplot ( mortality~ attend|type,
>>> pch=95,cex=5,col=2,
>>> par.settings=list(
>>> box.rectangle = list(col = "transparent"),
>>> box.umbrella = list(col = "transparent"),
>>> plot.symbol = list(col = "transparent")
>>> ),
>>> panel=function(x,y,...){
>>> panel.grid(lty=5)
>>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>>> panel.bwplot(x,y,...)
>>> },
>>> data = x,aspect=2:1,layout=c(3,1))
>>>
>>>
>>> However, I am interested in also learning how to do it in xyplot as well.  I
>>> wasn’t able to follow the last two set of instructions (condition on
>>> packet.number and loop over segments), wondering if I can ask for your help
>>> for the correct code (my attempt resulted in all three mean lines within
>>> each panel):
>>>
>>> x11(height=8,width=11)
>>> xyplot ( mortality ~ attend|type,
>>> panel=function(x,y)
>>> {
>>> panel.grid(lty=5)
>>> panel.xyplot(x,y,pch=16,jitter.x=TRUE,col=1)
>>> for(i in 1:3)
>>> {
>>> panel.segments(x0=c(0.7, 1.7),
>>> x1=c(1.3, 2.3),
>>> y0=c(tapply(x$mortality[x$type==i], x$attend[x$type==i], median)),
>>> y1=c(tapply(x$mortality[x$type==i], x$attend[x$type==i],
>>> median)),col="red",lwd=4)
>>> }
>>> },
>>> data = x,aspect=2:1,layout=c(3,1))
>>>
>>> thank you again. I also found your info on data.frame useful.
>>
>> I haven't figured out how to do it with the interaction of 'attend' and ''type' from inside xyplot. I'm thinking I might be able to succeed using trellis.focus() to address separate "columns" within a particular panel.
>>
>> This will draw  segments at (1,20) and (2,30) without resorting to low level grid/viewport stuff.
>>
>> trellis.unfocus(); trellis.focus("panel", 1, 1)
>> do.call("panel.segments", list(x0=1,y0=20,x1=1.2, y1=20))
>> trellis.unfocus()
>> trellis.unfocus(); trellis.focus("panel", 1, 1)
>> do.call("panel.segments", list(x0=2,y0=30,x1=2.2, y1=30))
>> trellis.unfocus()
>>
>> --
>> David
>>
>>
>> David Winsemius, MD
>> West Hartford, CT

John Maindonald             email: [hidden email]
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Mathematics & Its Applications, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
http://www.maths.anu.edu.au/~johnm


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Loading...