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") Thank you in advance! -- Sebasti?n Daza
On 2012-03-25 21:20, Sebasti?n Daza wrote:> 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! >
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!>>> >>> 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-- Sebasti?n Daza