[R] Need help with xyplot

Dennis Murphy djmuser at gmail.com
Mon Aug 8 21:58:32 CEST 2011


Hi:

Try this:

# Obtained from 'dput(dd)'
dd <- structure(list(AgeRange = structure(c(1L, 1L, 2L, 2L, 3L, 3L),
.Label = c("10-12",
"15-18", "30-40"), class = "factor"), AgeOfPerson = c(11L, 12L,
13L, 14L, 33L, 36L), PersonNo = 1:6, Year0 = c(0L, 0L, 1L, 1L,
3L, 4L), Year1 = c(1L, 1L, 2L, 3L, 5L, 4L), Year2 = c(2L, 2L,
3L, 4L, 5L, 4L), Year3 = c(2L, 2L, 4L, 5L, 6L, 4L), Year4 = c(3L,
3L, 6L, 7L, 8L, 4L), Year5 = c(3L, 3L, 7L, 7L, 9L, 4L)), .Names = c("AgeRange",
"AgeOfPerson", "PersonNo", "Year0", "Year1", "Year2", "Year3",
"Year4", "Year5"), class = "data.frame", row.names = c(NA, -6L
))

library('reshape')
library('lattice')
# Reshape data from wide to long
md <- melt(dd, id.vars = c('AgeRange', 'AgeOfPerson', 'PersonNo'))
# Make variable numeric by stripping off all but the last
# character and coercing it to numeric
md$variable <- as.numeric(substr(as.character(md$variable), 5, 5))

mykey = list(title="Age of Person",
             corner = c(1, 1),
             x = 0.85, y = 0.9, cex = 1.0,
             text = list(as.character(unique(md$AgeOfPerson))),
             lines = list(col = 1:6))
xyplot(value ~ variable | AgeRange, data = md, group = PersonNo, type
= c('p', 'l'),
        col = 1:6, col.lines = 1:6, key = mykey)

Here's my question: Why do you have a 15-18 age group with persons of
age 13 and 14 in it?

HTH,
Dennis

On Wed, Aug 3, 2011 at 2:06 PM,  <aziz4 at illinois.edu> wrote:
>
> Consider I have the following data:
>
> AgeRange        AgeOfPerson     PersonNo        FriendsAtYear0  FriendsAtYear1  FriendsAtYear2  FriendsAtYear3  FriendsAtYear4  FriendsAtYear5
> 10 - 12 11      1       0       1       2       2       3       3
> 10 - 12 12      2       0       1       2       2       3       3
> 15 - 18 13      3       1       2       3       4       6       7
> 15 - 18 14      4       1       3       4       5       7       7
> 30 - 40 33      5       3       5       5       6       8       9
> 30 - 40 36      6       4       4       4       4       4       4
>
> I want to plot the number of friends against number of years, as to show how friendships grew over time. Also, I want to group the graphs by AgeRange of persons and also color them with respect to the Age of Persons. So far, I have this code:
>
>
> FilePath = "C:\\Documents and Settings\\All Users\\Documents\\Desktop\\Fayez\\ResearchWork\\Pajek_Work\\Program\\2011-03-07-Wed-FriMeetingApr15\\RPractice\\"
>
> NumberOfyears <- c(0, 1, 2, 3, 4 ,5);
>
> File2Open = paste(FilePath, "FriendshipNetExample.txt", sep = "")
> #               print(File2Open)
>
> DataTable = read.table(File2Open, header = TRUE, sep = "\t")
> #               print(DataTable)
>
> print(xyplot(FriendsAtYear0 + FriendsAtYear1 + FriendsAtYear2 + FriendsAtYear3 + FriendsAtYear4 + FriendsAtYear5
>  ~ AgeOfPerson | AgeRange, data = DataTable, xlab = "Number of Years (0 to 5)", ylab = "Number of Friends", main = "Number of Friends vs. Years",
>  #aspect = "xy", # calculate an optimal aspect ratio
>  panel = function(x,y) {
>        panel.grid();
>        if (10 <= x && x < 12)  panel.xyplot(x,y,col="red");
>        if (15 <= x && x < 18)  panel.xyplot(x,y,col="salmon");
>        if (30 <= x && x < 40)  panel.xyplot(x,y,col="maroon");
> } ) )
>
> But it obviously does not serve the purpose. Urgent help would be most appreciated.
>
> Best,
> Fayez
> Grad student - UIUC
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list