R中的格子xyplot - 如何绘制同一主题的多个观察

时间:2014-04-24 12:49:26

标签: r lattice

我有一个如下所示的数据集,我想在R中使用格子xyplot绘制它:

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',
      )
)

正如您所看到的,主题2有多个观察结果(加入第一个序列的最后一个OBS点和下一个序列的第一个OBS点)

  • 如何指定xyplot我想要为主题2从0到3小时两次绘制,从而将每个序列视为一个单独的个体,从而产生3个单独的行?

  • 如何指定要绘制的单个主题(例如,我只想查看主题1的情节)?

我很想在R中绘图,所以请将您推荐给您认为更适合这些目的的其他套餐。

提前谢谢。

/ ykl

1 个答案:

答案 0 :(得分:0)

1)要拆分主题2的两次访问,您只需在ID参数中粘贴VISITNUMgroups=就可以创建一个新的分组变量:

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

仅查看一个组使用subset参数:

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