Re: [R] multiple lty on same panel in xyplot

2009-08-09 Thread Duncan Mackay

Hi Deepayan

Thank you very much for pointing out my mistake.

I had another go now with superpose.symbol and with several other 
combinations to see what could be done. All went well.


Regards

Duncan Mackay


At 13:19 10/08/2009, you wrote:

On 8/7/09, Duncan Mackay  wrote:
> Hi RUsers
>
>  I like to keep the plots self contained and avoid changing the current
> device parameters by using the par.settings.
>  To see what I could achieve by using par settings I tried the 
following and

> several variants but could not get black points.
>
>  xyplot(yM + yF ~ x,
> panel = panel.superpose,
> type = c("l", "p"),
> distribute.type = TRUE,
> par.settings = list(superpose.line = list(lty = c(1,2),
>   col = 
c("black","black")),

> superpose.points = list(pch = c(1,1), col =
> c("black","black")),

'superpose.points' should be 'superpose.symbol'.

A convenient shortcut is

   par.settings = simpleTheme(col = "black", lty = c(1, 2), pch = 1)

Try

str(simpleTheme(col = "black", lty = c(1, 2), pch = 1))

to see what that does.

-Deepayan

> plot.symbol = list(pch = c(1,1), col =
> c("black","black"))
> ),
> key = list(text = list(c("male", "female")),
>lines = Rows(pset$superpose.line, 1:2),
>pch = 1,
>type = c("l", "p")))
>
>  What am I missing? Does the points reference refer to Grid settings?
>
>  Regards


__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-09 Thread Deepayan Sarkar
On 8/7/09, Duncan Mackay  wrote:
> Hi RUsers
>
>  I like to keep the plots self contained and avoid changing the current
> device parameters by using the par.settings.
>  To see what I could achieve by using par settings I tried the following and
> several variants but could not get black points.
>
>  xyplot(yM + yF ~ x,
> panel = panel.superpose,
> type = c("l", "p"),
> distribute.type = TRUE,
> par.settings = list(superpose.line = list(lty = c(1,2),
>   col = c("black","black")),
> superpose.points = list(pch = c(1,1), col =
> c("black","black")),

'superpose.points' should be 'superpose.symbol'.

A convenient shortcut is

   par.settings = simpleTheme(col = "black", lty = c(1, 2), pch = 1)

Try

str(simpleTheme(col = "black", lty = c(1, 2), pch = 1))

to see what that does.

-Deepayan

> plot.symbol = list(pch = c(1,1), col =
> c("black","black"))
> ),
> key = list(text = list(c("male", "female")),
>lines = Rows(pset$superpose.line, 1:2),
>pch = 1,
>type = c("l", "p")))
>
>  What am I missing? Does the points reference refer to Grid settings?
>
>  Regards

__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-08 Thread Duncan Mackay
Hi Jake

the pset is the same as that of Deepayan Sarkar's. I must not have copied 
all the code as I thought and did not see that there was no pset

Basically I am trying to keep everything contained within the xyplot and 
avoid changing the device settings.
I frequently use par.settings = list( ...  and put any amendments that I 
need from trellis.par.get() there-
very easy to get an idea of what is where by names(trellis.par.get()) and 
then calling the particular name that you want to see what to change

Regards

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2350
Email Home: mac...@northnet.com.au



At 04:31 9/08/2009, you wrote:
>I tried pasting your code into R and got an error on pset:
>
> > xyplot(yM + yF ~ x,
>+panel = panel.superpose,
>+type = c("l", "p"),
>+distribute.type = TRUE,
>+par.settings = list(superpose.line = list(lty = c(1,2),
>+  col = c("black","black")),
>+superpose.points = list(pch = c(1,1), col = 
>c("black","black")),
>+plot.symbol = list(pch = c(1,1), col = 
>c("black","black"))
>+),
>+key = list(text = list(c("male", "female")),
>+   lines = Rows(pset$superpose.line, 1:2),
>+   pch = 1,
>+   type = c("l", "p")))
>Error in Rows(pset$superpose.line, 1:2) : object "pset" not found
>
>Jake Wegelin
>
>
>
>On Fri, Aug 7, 2009 at 6:53 PM, Duncan Mackay 
><mac...@northnet.com.au> wrote:
>Hi RUsers
>
>I like to keep the plots self contained and avoid changing the current 
>device parameters by using the par.settings.
>To see what I could achieve by using par settings I tried the following 
>and several variants but could not get black points.
>
>xyplot(yM + yF ~ x,
>panel = panel.superpose,
>type = c("l", "p"),
>distribute.type = TRUE,
>par.settings = list(superpose.line = list(lty = c(1,2),
>  col = c("black","black")),
>superpose.points = list(pch = c(1,1), col = 
> c("black","black")),
>plot.symbol = list(pch = c(1,1), col = 
> c("black","black"))
>),
>key = list(text = list(c("male", "female")),
>   lines = Rows(pset$superpose.line, 1:2),
>   pch = 1,
>   type = c("l", "p")))
>
>What am I missing? Does the points reference refer to Grid settings?
>
>Regards
>
>Duncan Mackay
>Department of Agronomy and Soil Science
>University of New England
>Armidale NSW 2350
>Email Home: mac...@northnet.com.au
>
>R version 2.9.1 (2009-06-26)
>i386-pc-mingw32
>
>attached base packages:
>[1] datasets  utils stats graphics  grDevices grid  methods   base
>other attached packages:
>[1] R.oo_1.4.8R.methodsS3_1.0.3 
>foreign_0.8-36chron_2.3-30  MASS_7.2-47   lattice_0.17-25
>
>
>
>At 11:48 6/08/2009, Deepayan Sarkar wrote:
>On Wed, Aug 5, 2009 at 2:24 PM, Jacob 
>Wegelin<jacob.wege...@gmail.com> wrote: > 
>On Wed, 5 Aug 2009, Deepayan Sarkar wrote:
> > >> On 8/5/09, Jacob Wegelin 
> <jacob.wege...@gmail.com> wrote:
> >>> I would like to use lattice graphics to plot multiple functions (or > 
> groups
> >>> Â or subpopulations) on the same plot region, using different line types
> > "lty"
> >>> Â or colors "col" to distinguish the functions (or groups).
> >>>
> >>> Â In traditional graphics, this seems straightforward: First plot all the
> > data
> >>> Â using 'type="n"', and subsequently execute a series of "points" or
> > "lines"
> >>> Â commands, one for each different group or function.
> >>>
> >>> Â What is the elegant way to do this using xyplot?
> >>>
> >>> Â To make this concrete, consider the following toy example:
> >>>
> >>> Â k<- 10
> >>> Â x<- (1:k)/3
> >>> Â yM<-6 + x^2
> >>> Â yF<-12 + x^(1.5)
> >>> Â xNA<-x[length(x)]
> >>>
> >>> Â # Insertion of NA row is necessary to prevent a meaningless line
> >>> Â # from being drawn from the females to the males across the entire 
> plot.
> >>>
> >>> Â DAT<-data.frame(
> >>> Â x=c(x, xNA, x) >>> Â , >>> Â y=c(yF, NA, yM) >>> Â , >>> Â sex=c( 
> rep(0, k ), 0, Â rep(1, k)) >>> Â ) >> >> It's much simpler in lattice, 
> and you don't need to play such tricks. > Option 1: >> >> xyplot(yM + yF 
> ~ x, type = "l", auto.key = list(points = FALSE, lines = > TRUE)) >> >> 
> and if you want to control lty etc: >> >> xyplot(yM + yF ~ x, type = "l", 
> auto.key = list(points = FALSE, lines = > TRUE), >> Â  Â  Â  par.settings 
> = simpleTheme(lty = c(2, 3))) >> >> >> Option 2 (a bit more work, but 
> less mysterious under the hood): >> >> DAT<- >> Â  Â data.frame(x = c(x, 
> x), y=c(yF, yM), >> Â  Â  Â  Â  Â  Â  Â  sex= rep(c("

Re: [R] multiple lty on same panel in xyplot

2009-08-07 Thread Duncan Mackay

Hi RUsers

I like to keep the plots self contained and avoid changing the current 
device parameters by using the par.settings.
To see what I could achieve by using par settings I tried the following and 
several variants but could not get black points.


xyplot(yM + yF ~ x,
panel = panel.superpose,
type = c("l", "p"),
distribute.type = TRUE,
par.settings = list(superpose.line = list(lty = c(1,2),
  col = c("black","black")),
superpose.points = list(pch = c(1,1), col = 
c("black","black")),
plot.symbol = list(pch = c(1,1), col = 
c("black","black"))

),
key = list(text = list(c("male", "female")),
   lines = Rows(pset$superpose.line, 1:2),
   pch = 1,
   type = c("l", "p")))

What am I missing? Does the points reference refer to Grid settings?

Regards

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2350
Email Home: mac...@northnet.com.au

R version 2.9.1 (2009-06-26)
i386-pc-mingw32

attached base packages:
[1] datasets  utils stats graphics  grDevices 
grid  methods   base

other attached packages:
[1] R.oo_1.4.8R.methodsS3_1.0.3 
foreign_0.8-36chron_2.3-30  MASS_7.2-47   lattice_0.17-25




At 11:48 6/08/2009, Deepayan Sarkar wrote:
On Wed, Aug 5, 2009 at 2:24 PM, Jacob Wegelin 
wrote: > On Wed, 5 Aug 2009, Deepayan Sarkar wrote:

> >> On 8/5/09, Jacob Wegelin  wrote:
>>> I would like to use lattice graphics to plot multiple functions (or > 
groups

>>> Â or subpopulations) on the same plot region, using different line types
> "lty"
>>> Â or colors "col" to distinguish the functions (or groups).
>>>
>>> Â In traditional graphics, this seems straightforward: First plot all 
the

> data
>>> Â using 'type="n"', and subsequently execute a series of "points" or
> "lines"
>>> Â commands, one for each different group or function.
>>>
>>> Â What is the elegant way to do this using xyplot?
>>>
>>> Â To make this concrete, consider the following toy example:
>>>
>>> Â k<- 10
>>> Â x<- (1:k)/3
>>> Â yM<-6 + x^2
>>> Â yF<-12 + x^(1.5)
>>> Â xNA<-x[length(x)]
>>>
>>> Â # Insertion of NA row is necessary to prevent a meaningless line
>>> Â # from being drawn from the females to the males across the entire 
plot.

>>>
>>> Â DAT<-data.frame(
>>> Â x=c(x, xNA, x) >>> Â , >>> Â y=c(yF, NA, yM) >>> Â , >>> Â sex=c( 
rep(0, k ), 0, Â rep(1, k)) >>> Â ) >> >> It's much simpler in lattice, 
and you don't need to play such tricks. > Option 1: >> >> xyplot(yM + yF 
~ x, type = "l", auto.key = list(points = FALSE, lines = > TRUE)) >> >> 
and if you want to control lty etc: >> >> xyplot(yM + yF ~ x, type = "l", 
auto.key = list(points = FALSE, lines = > TRUE), >> Â  Â  Â  par.settings 
= simpleTheme(lty = c(2, 3))) >> >> >> Option 2 (a bit more work, but 
less mysterious under the hood): >> >> DAT<- >> Â  Â data.frame(x = c(x, 
x), y=c(yF, yM), >> Â  Â  Â  Â  Â  Â  Â  sex= rep(c("Female", "Male"), 
each = length(x))) >> >> xyplot(y ~ x, data = DAT, groups = sex, type = 
"l") > > Dear Bill and Deepayan, > > Thanks. This is helpful. Where can 
one find a thorough documentation of all > these features like 
par.settings, simpleTheme, the options for where to > place the legend or 
"key", auto.key, the different locations besides "top" > where one can 
place the "auto.key", etc.? Â I don't think this is all clearly > laid 
out in the R help files or latticeLab.pdf. (Almost) everything is 
mentioned in the help pages (?Lattice is a good place to start). Of 
course finding the thing you are looking for is another matter. The book 
does try to present things more systematically. > But using your hints I 
found that the following worked: > > xyplot( > y ~ x > , groups= ~ sex > 
, type="l" > , auto.key = list(columns=2, points = FALSE, lines = TRUE) > 
, par.settings = simpleTheme(lty = c(1, 2), col="black") > , data=DAT > 
) > > Now, how would I use lattice tools to plot males with a line and 
females > with points--and still get an informative autokey?


xyplot(yM + yF ~ x,
  type = c("l", "p"),
  distribute.type = TRUE,
  par.settings = simpleTheme(lty = c(1, 2), col="black"),
 auto.key = list(points = FALSE, lines = TRUE, type = c("l", "p")))

...but this is pretty much impossible to figure out for a beginner.
On the other hand, reading the documentation carefully should lead you to 
the following, which is almost there:


pset <- simpleTheme(lty = c(1, 2), col="black")
xyplot(yM + yF ~ x,
panel = panel.superpose,
type = c("l", "p"),
distribute.type = TRUE,
par.settings = pset,
key = list(text = list(c("male", "female")),
   lines = Rows(pset$superpose.line, 1:2),
   pch = 1,
   type = c("l", "p")))

-Deepayan

Re: [R] multiple lty on same panel in xyplot

2009-08-06 Thread Patrick Connolly
On Wed, 05-Aug-2009 at 05:24PM -0400, Jacob Wegelin wrote:


|> 
|> Dear Bill and Deepayan,
|> 
|> Thanks. This is helpful. Where can one find a thorough
|> documentation of all these features like par.settings, simpleTheme,
|> the options for where to place the legend or "key", auto.key, the
|> different locations besides "top" where one can place the
|> "auto.key", etc.?  I don't think this is all clearly laid out in
|> the R help files or latticeLab.pdf. 

It's not immediately obvious, but you can find a lot of information in
the rather large help file for xyplot -- applicable even if you're
using bwplot or dotplot or whatever.  I often search for text in there
such as "key" which leads a long way down to options for "space" which
can take values of "top", "bottom", etc.  There are many options and
perhaps they're easier to find laid out in the book.

HTH

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-05 Thread Deepayan Sarkar
On Wed, Aug 5, 2009 at 2:24 PM, Jacob Wegelin wrote:
> On Wed, 5 Aug 2009, Deepayan Sarkar wrote:
>
>> On 8/5/09, Jacob Wegelin  wrote:
>>> I would like to use lattice graphics to plot multiple functions (or
> groups
>>>  or subpopulations) on the same plot region, using different line types
> "lty"
>>>  or colors "col" to distinguish the functions (or groups).
>>>
>>>  In traditional graphics, this seems straightforward: First plot all the
> data
>>>  using 'type="n"', and subsequently execute a series of "points" or
> "lines"
>>>  commands, one for each different group or function.
>>>
>>>  What is the elegant way to do this using xyplot?
>>>
>>>  To make this concrete, consider the following toy example:
>>>
>>>  k<- 10
>>>  x<- (1:k)/3
>>>  yM<-6 + x^2
>>>  yF<-12 + x^(1.5)
>>>  xNA<-x[length(x)]
>>>
>>>  # Insertion of NA row is necessary to prevent a meaningless line
>>>  # from being drawn from the females to the males across the entire plot.
>>>
>>>  DAT<-data.frame(
>>>  x=c(x, xNA, x)
>>>  ,
>>>  y=c(yF, NA, yM)
>>>  ,
>>>  sex=c( rep(0, k ), 0,  rep(1, k))
>>>  )
>>
>> It's much simpler in lattice, and you don't need to play such tricks.
> Option 1:
>>
>> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
> TRUE))
>>
>> and if you want to control lty etc:
>>
>> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
> TRUE),
>>       par.settings = simpleTheme(lty = c(2, 3)))
>>
>>
>> Option 2 (a bit more work, but less mysterious under the hood):
>>
>> DAT<-
>>    data.frame(x = c(x, x), y=c(yF, yM),
>>               sex= rep(c("Female", "Male"), each = length(x)))
>>
>> xyplot(y ~ x, data = DAT, groups = sex, type = "l")
>
> Dear Bill and Deepayan,
>
> Thanks. This is helpful. Where can one find a thorough documentation of all
> these features like par.settings, simpleTheme, the options for where to
> place the legend or "key", auto.key, the different locations besides "top"
> where one can place the "auto.key", etc.?  I don't think this is all clearly
> laid out in the R help files or latticeLab.pdf.

(Almost) everything is mentioned in the help pages (?Lattice is a good
place to start). Of course finding the thing you are looking for is
another matter. The book does try to present things more
systematically.

> But using your hints I found that the following worked:
>
> xyplot(
> y ~ x
> , groups= ~ sex
> , type="l"
> , auto.key = list(columns=2, points = FALSE, lines = TRUE)
> , par.settings = simpleTheme(lty = c(1, 2), col="black")
> , data=DAT
> )
>
> Now, how would I use lattice tools to plot males with a line and females
> with points--and still get an informative autokey?

xyplot(yM + yF ~ x, type = c("l", "p"), distribute.type = TRUE,
   par.settings = simpleTheme(lty = c(1, 2), col="black"),
   auto.key = list(points = FALSE, lines = TRUE, type = c("l", "p")))


...but this is pretty much impossible to figure out for a beginner.
On the other hand, reading the documentation carefully should lead you
to the following, which is almost there:


pset <- simpleTheme(lty = c(1, 2), col="black")

xyplot(yM + yF ~ x,
   panel = panel.superpose,
   type = c("l", "p"),
   distribute.type = TRUE,
   par.settings = pset,
   key = list(text = list(c("male", "female")),
  lines = Rows(pset$superpose.line, 1:2),
  pch = 1,
  type = c("l", "p")))

-Deepayan

__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-05 Thread Jacob Wegelin
On Wed, 5 Aug 2009, Deepayan Sarkar wrote:

> On 8/5/09, Jacob Wegelin  wrote:
>> I would like to use lattice graphics to plot multiple functions (or
groups
>>  or subpopulations) on the same plot region, using different line types
"lty"
>>  or colors "col" to distinguish the functions (or groups).
>>
>>  In traditional graphics, this seems straightforward: First plot all the
data
>>  using 'type="n"', and subsequently execute a series of "points" or
"lines"
>>  commands, one for each different group or function.
>>
>>  What is the elegant way to do this using xyplot?
>>
>>  To make this concrete, consider the following toy example:
>>
>>  k<- 10
>>  x<- (1:k)/3
>>  yM<-6 + x^2
>>  yF<-12 + x^(1.5)
>>  xNA<-x[length(x)]
>>
>>  # Insertion of NA row is necessary to prevent a meaningless line
>>  # from being drawn from the females to the males across the entire plot.
>>
>>  DAT<-data.frame(
>>  x=c(x, xNA, x)
>>  ,
>>  y=c(yF, NA, yM)
>>  ,
>>  sex=c( rep(0, k ), 0,  rep(1, k))
>>  )
>
> It's much simpler in lattice, and you don't need to play such tricks.
Option 1:
>
> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
TRUE))
>
> and if you want to control lty etc:
>
> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
TRUE),
>   par.settings = simpleTheme(lty = c(2, 3)))
>
>
> Option 2 (a bit more work, but less mysterious under the hood):
>
> DAT<-
>data.frame(x = c(x, x), y=c(yF, yM),
>   sex= rep(c("Female", "Male"), each = length(x)))
>
> xyplot(y ~ x, data = DAT, groups = sex, type = "l")

Dear Bill and Deepayan,

Thanks. This is helpful. Where can one find a thorough documentation of all
these features like par.settings, simpleTheme, the options for where to
place the legend or "key", auto.key, the different locations besides "top"
where one can place the "auto.key", etc.?  I don't think this is all clearly
laid out in the R help files or latticeLab.pdf. But using your hints I found
that the following worked:


xyplot(
y ~ x
, groups= ~ sex
, type="l"
, auto.key = list(columns=2, points = FALSE, lines = TRUE)
, par.settings = simpleTheme(lty = c(1, 2), col="black")
, data=DAT
)

Now, how would I use lattice tools to plot males with a line and females
with points--and still get an informative autokey?

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.
E-mail: jwege...@vcu.edu
URL: http://www.people.vcu.edu/~jwegelin

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-05 Thread Deepayan Sarkar
On 8/5/09, Jacob Wegelin  wrote:
> I would like to use lattice graphics to plot multiple functions (or groups
>  or subpopulations) on the same plot region, using different line types "lty"
>  or colors "col" to distinguish the functions (or groups).
>
>  In traditional graphics, this seems straightforward: First plot all the data
>  using 'type="n"', and subsequently execute a series of "points" or "lines"
>  commands, one for each different group or function.
>
>  What is the elegant way to do this using xyplot?
>
>  To make this concrete, consider the following toy example:
>
>  k<- 10
>  x<- (1:k)/3
>  yM<-6 + x^2
>  yF<-12 + x^(1.5)
>  xNA<-x[length(x)]
>
>  # Insertion of NA row is necessary to prevent a meaningless line
>  # from being drawn from the females to the males across the entire plot.
>
>  DAT<-data.frame(
>  x=c(x, xNA, x)
>  ,
>  y=c(yF, NA, yM)
>  ,
>  sex=c( rep(0, k ), 0,  rep(1, k))
>  )

It's much simpler in lattice, and you don't need to play such tricks. Option 1:

xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines = TRUE))

and if you want to control lty etc:

xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines = TRUE),
   par.settings = simpleTheme(lty = c(2, 3)))


Option 2 (a bit more work, but less mysterious under the hood):

DAT<-
data.frame(x = c(x, x), y=c(yF, yM),
   sex= rep(c("Female", "Male"), each = length(x)))

xyplot(y ~ x, data = DAT, groups = sex, type = "l")

-Deepayan

[...]

>  This draws both men and women in the same color and line type. Instead, I
>  want to specify different "col" and "lty" values for the two groups.
>
>  More generally, does a reference exist that explains this kind of thing,
>  with examples? I have not yet found an answer to this question in Paul
>  Murrell's book. Does Deepayan Sarkar's _Lattice_ go into that kind of
>  detail?

Yes, but for a shorter introduction, see

http://www.bioconductor.org/workshops/2008/PHSIntro/latticeLab.pdf

-Deepayan

__
R-help@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.


Re: [R] multiple lty on same panel in xyplot

2009-08-05 Thread Jacob Wegelin
On Wed, Aug 5, 2009 at 1:30 PM, Jacob Wegelin wrote:

> I would like to use lattice graphics to plot multiple functions (or groups
> or subpopulations) on the same plot region, using different line types "lty"
> or colors "col" to distinguish the functions (or groups).
>
> In traditional graphics, this seems straightforward: First plot all the
> data using 'type="n"', and subsequently execute a series of "points" or
> "lines" commands, one for each different group or function.
>
> What is the elegant way to do this using xyplot?
>

> To make this concrete, consider the following toy example:
>
> k<- 10
>  x<- (1:k)/3
> yM<-6 + x^2
> yF<-12 + x^(1.5)
>  xNA<-x[length(x)]
>
> # Insertion of NA row is necessary to prevent a meaningless line
> # from being drawn from the females to the males across the entire plot.
>
> DAT<-data.frame(
>  x=c(x, xNA, x)
> ,
> y=c(yF, NA, yM)
>  ,
> sex=c( rep(0, k ), 0,  rep(1, k))
> )
>
> library("lattice")
>

Solution (easier than I had imagined):

   myPanel<-function( x, y ) {
  panel.xyplot(x, y, type="n")
  llines( x[DAT$sex==0], y[DAT$sex==0], col="red", lty=1 )
  llines( x[DAT$sex==1], y[DAT$sex==1], col="blue", lty=2 )
   }

   xyplot(
  y ~ x
  , data=DAT
  , type="l"
  , panel=myPanel
   )

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.
E-mail: jwege...@vcu.edu
URL: http://www.people.vcu.edu/~jwegelin

[[alternative HTML version deleted]]

__
R-help@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.