[R] trellis plot
Peter Ehlers
ehlers at ucalgary.ca
Mon Mar 26 21:38:55 CEST 2012
On 2012-03-26 09:09, Sebastián Daza wrote:
> Thank you Peter. The problem with you solution is that it doesn't
> represent the actual values of disruption. Look this example:
>
> person<- rep(1:2, each=4)
> income<- c(100, 120, 150, 200, 90, 100,120, 150)
> disruption<- c(0,0,0,1,0,1,1,0)
> time<- rep(c(1:4),2)
> dat<- as.data.frame(cbind(person,time, income, disruption))
>
> mycols<- c(2, 5)
> library(lattice)
> xyplot(income~time|as.factor(person),data=dat,
> type=c("p","g","o"), col.line="black", col.symbol =
> mycols[dat$disruption+1],
> xlab="Time",
> ylab="Familiar Income")
>
> I am looking for a way to get the colors of dots according to actual
> disruption values, and at the same time to draw the lines between all
> dots for each person. Thank you!
Okay, my approach was too simple and dependent on the particular
disruption sequence you first gave.
What you actually need is a panel-specific 'disruption' vector giving
the colours. That can be done with 'subscripts' (which pick out the
values of disruption that apply to a given panel):
xyplot(income~time|as.factor(person),data=dat
, type=c("b","g")
, panel = function(..., subscripts){
panel.xyplot(...,
col.symbol =
mycols[dat$disruption[subscripts] + 1])})
BTW, your method of creating the data frame is unnecessarily complex.
It would suffice to use
dat <- data.frame(person, time, .... etc)
Peter Ehlers
>
>
>>>>
>>>> Hi everyone,
>>>> I am just trying to figure out how to do a xyplot where in addition to
>>>> dots and lines I can change dots' colors according to an individual
>>>> variable (e.g., marital disruption across time, a dummy 0/1). When I
>>>> use "groups" specification (see below), I get two different lines for
>>>> each individual based on groups, and what I want is to get one line
>>>> connecting dots, and different dots' colors according to marital
>>>> disruption.
>>>>
>>>> Any ideas about how to do that?
>>>>
>>>> person<- rep(1:2, each=4)
>>>> income<- c(100, 120, 150, 200, 90, 100,120, 150)
>>>> disruption<- rep(c(0,1), 4)
>>>> time<- rep(c(1:4),2)
>>>> dat<- as.data.frame(cbind(person,time, income, disruption))
>>>>
>>>>
>>>> library(lattice)
>>>> xyplot(income~time|as.factor(person),data=dat,
>>>> type=c("p","g","o"), col.line="black",
>>>> xlab="Time",
>>>> ylab="Familiar Income")
>>>>
>>>> # I just want to change dots' colors according to disruption, not to
>>>> get two different lines:
>>>>
>>>> xyplot(income~time|as.factor(person),data=dat,
>>>> type=c("p","g","o"), col.line="black", groups=disruption,
>>>> xlab="Time",
>>>> ylab="Familiar Income")
>>>>
>>>
>>> If I understand correctly what you're after, try adding the
>>> 'col.symbol' argument instead of the 'groups' argument.
>>>
>>> col.symbol = disruption + 1
>>>
>>> or, better, for arbitrary colours:
>>>
>>> mycols<- c(2, 5)
>>> xyplot(....,
>>> col.symbol = mycols[disruption + 1],
>>> ....)
>>>
>>> Peter Ehlers
>>>
>>>>
>>>>
>>>> Thank you in advance!
>>>>
>>>
>>
>>
>>
>> --
>> Sebastián Daza
>
>
>
More information about the R-help
mailing list