Re: [R] XYPLOT - GROUPING WITH TWO CATEGORICAL VARIABLES

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Mon, 07 Mar 2011 14:18:53 -0800

David, David, David...you forgot the solid and dashed lines :) It's OK, it gives me an excuse to look at this from a slightly different angle. [The intersection() function is a *really* good trick, BTW - thanks for the reminder.]

Back to the OP. Let's re-read the data:

dat=data.frame(Age = rep(c(30, 40, 50), each = 8),

               Period = rep(2005:2008 ,6),
               Rate = 1:24,
               Sex = rep(rep(c('F', 'M'), each = 4), 3)
dat$Period <- factor(dat$Period, levels = c(2005:2008))

# Go one step further and create a Sex * Age interaction factor by hand, using the paste() function:
dat$SexAge <- with(dat, paste(Sex, Age, sep = ' '))

# Now use it in the xyplot...
xyplot(Rate ~ Period, data = dat, groups = SexAge, type = 'b',

        lty = rep(c('solid', 'dashed'), each = 3),
        col = rep(1:3, 2), col.lines = rep(1:3, 2),
        key = list(space = 'right',
                   title = 'Sex-Age', cex.title = 1.1,
                   text = list(levels(dat$SexAge), cex = 0.8),
                   lines = list(lty = rep(c('solid', 'dashed'), each = 3),
                                col = rep(1:3, 2))
                  ),
        xlab = "Period", ylab = "Rate",ylim=c(0,25),
        main = "Mortality Rates per 100000 inhabitants - Women & Men",
        scales=list(tck=c(1,0))
      )

Dashed lines tend to show up better than do dotted lines. If you really want the dotted lines instead, substitute 'dotted' for 'dashed'.

This way isn't any better than what David showed you, but as there are multiple ways to do things in R, creation of a new factor in the data to handle age-gender combinations is one method that allows you to exercise more control over the set of values produced, particularly if you intend to produce a legend. If you want different colors, create a vector of length 3, say c('red', 'green', 'blue') [hoping your viewer isn't color blind] and substitute it for 1:3 in the col and col.lines statements in the xyplot() call, as well as in the col argument for lines = in the key() statement.

...BTW, I echo David's editorial comments and would suggest that you read the Posting Guide..carefully. (See the bottom of this message for the link.)

Dennis

On Mon, Mar 7, 2011 at 6:18 AM, David Winsemius <dwinsemius_at_comcast.net>wrote:

>
> 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
>
> ______________________________________________
> 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.
>

        [[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. Received on Mon 07 Mar 2011 - 22:42:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Mar 2011 - 23:20:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive