[R] multiple lty on same panel in xyplot
Duncan Mackay
mackay at northnet.com.au
Sat Aug 8 00:53:58 CEST 2009
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: mackay at 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.8 R.methodsS3_1.0.3
foreign_0.8-36 chron_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.wegelin at gmail.com>
>wrote: > On Wed, 5 Aug 2009, Deepayan Sarkar wrote:
> > >> On 8/5/09, Jacob Wegelin <jacob.wegelin at 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("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 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