I'm trying to create an xyplot from the following sample of my dataset:
dput(head(trainsamp,25))
structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("Endurance", "Strength", "Concurrent"), class = "factor"),
time = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Pre", "Post"), class = "factor"), FFM = c(55.883166,
56.658898, 57.933614, 65.295368, 63.199246, 65.906551, 51.201461,
49.218984, 53.773112, 71.202309, 53.042409, 50.749445, 50.771442,
54.768907, 52.981304, 56.578874, 55.442133, 51.263485, 54.639979,
60.626251, 59.256806, 63.780228, 67.094356, 55.860919, 59.185318
), id = 1:25), .Names = c("group", "time", "FFM", "id"), row.names = c("1.1",
"2.1", "3.1", "4.1", "5.1", "6.1", "7.1", "8.1", "9.1", "10.1",
"11.1", "12.1", "13.1", "14.1", "15.1", "16.1", "17.1", "18.1",
"19.1", "20.1", "21.1", "22.1", "23.1", "24.1", "25.1"), class = "data.frame")
I've tried the following code:
library(lattice)
xyplot(trainsamp$FFM~trainsamp$time|trainsamp$id,group=trainsamp$group,type="l",col=c("blue","red","black"),
ylab="Mean Fat Free Mass (kg)",xlab="Time",
main="Individual Trajectories")
legend(locator(1),legend=levels(group),lty=1,col=c("blue","red","black"))
And I continue to get a graph but the legend seems to be missing and I receive the following error:
Error in levels(group) : object 'group' not found
I want to avoid using the attach
command. Can anyone tell why I'm having the problem with "group"?
This is a late answer, but part of your problem is that legend()
is base graphics and xyplot
is from lattice
and these two do not mix well. You can just use the auto.key=T
argument to add a key. Here is a somewhat cleaned up version of your code:
xyplot(FFM~time | id, group=group, data=trainsamp, type="p",
col=c("blue","red","black"), ylab="Mean Fat Free Mass (kg)",
xlab="Time",main="Individual Trajectories", auto.key=T)