Marcos Prunello
2011-Mar-07 13:37 UTC
[R] XYPLOT - GROUPING WITH TWO CATEGORICAL VARIABLES
Hi! I have a dataframe like this: dat=data.frame(Age=c(rep(30,8),rep(40,8),rep(50,8)),Period=rep(seq(2005,2008,1),3),Rate=c(seq(1,8,1),seq(9,16,1),seq(17,24,1)),Sex=rep(c(rep(0,4),rep(1,4)),3))attach(dat)dat Age Period Rate Sex1 30 2005 1 02 30 2006 2 03 30 2007 3 04 30 2008 4 05 30 2005 5 16 30 2006 6 17 30 2007 7 18 30 2008 8 19 40 2005 9 010 40 2006 10 011 40 2007 11 012 40 2008 12 013 40 2005 13 114 40 2006 14 115 40 2007 15 116 40 2008 16 117 50 2005 17 018 50 2006 18 019 50 2007 19 020 50 2008 20 021 50 2005 21 122 50 2006 22 123 50 2007 23 124 50 2008 24 1 And I can do these separated graphs by sex: xyplot(Rate ~ Period, data=subset(dat,Sex==0), groups = Age, type = "b", auto.key = list(cex=0.8,border=TRUE,size=3,cex.title=1,space = "right", title="Age", points = FALSE , lines = TRUE), xlab = "Period", ylab = "Rate",ylim=c(0,25), main = "Mortality Rates for 100000 inhabitants - Men", scales=list(tck=c(1,0)) )xyplot(Rate ~ Period, data=subset(dat,Sex==1), groups = Age, type = "b", auto.key = list(cex=0.8,border=TRUE,size=3,cex.title=1,space = "right", title="Age", points = FALSE , lines = TRUE), xlab = "Period", ylab = "Rate",ylim=c(0,25), main = "Mortality Rates for 100000 inhabitants - Women", scales=list(tck=c(1,0)) ) BUT I WANT THEM IN THE SAME GRAPH, IN THE SAME PANEL, TO BE COMPARED, FOR EXAMPLE WITH THE SAME COLOUR FOR THE SAME AGE GROUPS,AND A COMPLETE LINE FOR SEX=0 AND A DOTTED LINE FOR SEX=1 I TRIED DIFFERENT THINGS BUT NOTHING WORKED FOR ME. I USED "PANEL" OPTIONS, OR XY.SUPERPOSE, BUT I DON`T KNOW HOW TO USE THOSE THINGS PROPERLY. THANK YOU!!! MARCOS (ARGENTINA) [[alternative HTML version deleted]]
David Winsemius
2011-Mar-07 14:18 UTC
[R] XYPLOT - GROUPING WITH TWO CATEGORICAL VARIABLES
On Mar 7, 2011, at 8:37 AM, Marcos Prunello wrote:> Hi! I have a dataframe like this: > dat > > data > .frame > (Age=c(rep(30,8),rep(40,8),rep(50,8)),Period=rep(seq(2005,2008,1), > 3 > ),Rate > =c(seq(1,8,1),seq(9,16,1),seq(17,24,1)),Sex=rep(c(rep(0,4),rep(1,4)), > 3))attach(dat)dat > Age Period Rate Sex1 30 2005 1 02 30 2006 2 > 03 30 2007 3 04 30 2008 4 05 30 2005 5 > 16 30 2006 6 17 30 2007 7 18 30 2008 8 > 19 40 2005 9 010 40 2006 10 011 40 2007 11 > 012 40 2008 12 013 40 2005 13 114 40 2006 14 > 115 40 2007 15 116 40 2008 16 117 50 2005 17 > 018 50 2006 18 019 50 2007 19 020 50 2008 20 > 021 50 2005 21 122 50 2006 22 123 50 2007 23 > 124 50 2008 24 1 > And I can do these separated graphs by sex: > xyplot(Rate ~ Period, data=subset(dat,Sex==0), groups = Age, > type = "b", auto.key = > list(cex=0.8,border=TRUE,size=3,cex.title=1,space = "right", > title="Age", points = FALSE , lines = TRUE), xlab = "Period", > ylab = "Rate",ylim=c(0,25), main = "Mortality Rates for 100000 > inhabitants - Men", scales=list(tck=c(1,0)) )xyplot(Rate ~ > Period, data=subset(dat,Sex==1), groups = Age, type = > "b", auto.key = > list(cex=0.8,border=TRUE,size=3,cex.title=1,space = "right", > title="Age", points = FALSE , lines = TRUE), xlab = "Period", > ylab = "Rate",ylim=c(0,25), main = "Mortality Rates for 100000 > inhabitants - Women", scales=list(tck=c(1,0)) ) > BUT I WANT THEM IN THE SAME GRAPH, IN THE SAME PANEL, TO BE > COMPARED, FOR EXAMPLE WITH THE SAME COLOUR FOR THE SAME AGE > GROUPS,AND A COMPLETE LINE FOR SEX=0 AND A DOTTED LINE FOR SEX=1 > I TRIED DIFFERENT THINGS BUT NOTHING WORKED FOR ME. I USED "PANEL" > OPTIONS, OR XY.SUPERPOSE, BUT I DON`T KNOW HOW TO USE THOSE THINGS > PROPERLY.You may need to adjust your netiquette. All caps is considered "shouting" (not to mention more difficult to read). Try: xyplot(Rate ~ Period, data=dat, groups = interaction(Age, Sex), type = "b", auto.key = list(cex=0.8,border=TRUE,size=3,cex.title=1, space = "right", title="Age.Sex", points = FALSE , lines = TRUE), xlab = "Period", ylab = "Rate",ylim=c(0,25), main = "Mortality Rates for 100000 inhabitants - Women & Men", scales=list(tck=c(1,0)) )> THANK YOU!!! > MARCOS (ARGENTINA) > > > > [[alternative HTML version deleted]]You should also learn to post in plain text. That way you linefeeds won't disappear. -- David Winsemius, MD West Hartford, CT