Question

I have a dataset which looks like the following and I want to plot it using lattice xyplot in R:

ID = c("1","1","1","1","1","1","1","2","2","2","2","2","2","2","2","2","2","2","2","2","2") 
TIME = c("0", "0.5", "1","1.5","2","2.5","3","0","0", "0.5","0.5", "1","1","1.5","1.5","2","2","2.5","2.5","3","3") 
OBS = c("0", "0.73", "0.98", "1.24", "2.06","2.56","4.01", "0", "0.03", "0.76", "0.85", "2.13","2.78","3.9", "4.1", "5.4", "5.6", "7.8", "8.0","8.4","8.8") 
VISITNUM = c("1","1","1","1","1","1","1","1","2", "1","2","1","2","1","2","1","2","1","2","1","2") 
DF = data.frame(ID, TIME, OBS, VISITNUM)       
DF <- DF[order(DF$ID, DF$VISITNUM, DF$TIME),]

library(lattice)
print(
  xyplot(OBS ~ TIME,
     groups = ID,
     data = DF,
     type = 'b',
      )
)

As you can see there are multiple observations on subject 2 (joining the last OBS point of the first sequence and the first OBS point of the next sequence)

  • How do I specify to xyplot that I want to plot from 0 to 3 hours twice for subject 2 and thus treat each sequence as a single individual, resulting in 3 separate lines?

  • How do I specify a single subject to be plotted (e.g. I want to only see the plot for subject 1)?

I am new to plotting in R, so please refer me to other packages you may deem more appropriate for these purposes.

Thank you in advance.

/ykl

Was it helpful?

Solution

1) To break apart the two visits for subject 2, you can make a new grouping variable simply by pasting ID and VISITNUM within the groups= argument like so:

xyplot(OBS ~ TIME,
  groups = paste(ID,VISITNUM)
  data = DF,
  type = 'b',
)

To only see one group use the subset argument:

xyplot(OBS ~ TIME,
  groups = paste(ID,VISITNUM)
  data = DF,
  subset = ID==1,
  type = 'b',
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top