[R] lattice: customising panel.segments using groups
Felix Andrews
felix at nfrac.org
Mon May 10 14:51:09 CEST 2010
On 9 May 2010 15:25, Ky Mathews <ky.mathews at sydney.edu.au> wrote:
> Hi,
>
>
>
> I want to customise the segments on an xyplot. Below is a simple example
> of what I'm trying to do...
>
>
>
> #Example dataset
>
> x <- c(-0.25, 0.25, 0.8)
>
> y <- c(-0.5, 0, 0.75)
>
> gp <- c("A", "I", "C")
>
> my.data <- cbind.data.frame(x,y,gp)
>
>
>
> #setting up the parameters to customise the lines with.
>
> ltype <- c(1,2,3)
>
> env.col <- c("red", "black", "blue")
>
> env.lwd <- c(1.25, 0.75, 1.25)
>
>
>
> # Lattice plot
>
> xyplot(y ~ x,
>
> data= my.data,
>
> groups = gp,
>
> type = "l",
>
> panel= panel.superpose,
>
> panel.groups = function(x,y, subscripts=subscripts,
> groups=groups,...){
>
> panel.segments(0, 0, x[groups], y[groups], lty = ltype[groups],
> lwd=env.lwd[groups], col = env.col[groups])})
The 'panel.groups' function is passed a special argument
'group.number', not 'groups'... (Also you do not need to subset 'x'
and 'y' because they are already subsetted by 'panel.superpose'.)
xyplot(y ~ x, my.data,
groups = gp, type = "l",
panel = panel.superpose,
panel.groups = function(x, y, group.number, ...) {
panel.segments(0, 0, x, y, lty = ltype[group.number],
lwd=env.lwd[group.number], col = env.col[group.number])})
...however, panel.superpose does this for you automatically, so you
only need to pass 'lty', 'lwd' and 'col' arguments to xyplot, and
these will be split up by group.number and passed on as single values.
For an even better solution, it is recommended to use par.settings
(or trellis.par.set for global settings), so that 'auto.key' will
work:
xyplot(y ~ x, my.data,
groups = gp, type = "l",
panel = panel.superpose,
panel.groups = function(x, y, ...)
panel.xyplot(c(0, x), c(0, y), ...),
par.settings = simpleTheme(col = c("red", "black", "blue"),
lwd = c(1.25, 0.75, 1.25), lty = c(1,2,3)),
auto.key = list(lines = TRUE, points = FALSE))
HTH
-Felix
>
>
>
>
> #The problem:
> I don't seem to have this quite right, as the resulting plot seems to
> ignore the lty, lwd, col that I set up.
>
> I tried to do this by using trellis.par.set/get but simply got confused.
>
>
>
> If I remove lty, lwd and col specifications I simply get all segments
> with the same attributes. i.e. it seems to be ignoring the "groups"
>
>
>
>
>
> #The answer I want:
>
> Segment1 has co-ordinates (0,0, x2=-0.25, y2 = -0.5) and be solid, red
> and of width 1.25
>
> Segment2 has co-ordinates (0,0, x2=0.25, y2 = 0) and be dashed, black
> and of width 0.75
>
> Segment3 has co-ordinates (0,0, x2=0.8, y2 = 0.75) and be dotted, blue
> and of width 1.25
>
>
>
> Any help is much appreciated.
>
>
>
> Thanks and regards,
>
> Ky Mathews
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
--
http://www.neurofractal.org/felix/
More information about the R-help
mailing list