2

I have two data frames:

df1 <- read.table(text = "
Time    Score   ID
 -83       19   C1
 -69       17   C2
 -55       15   C3
 -28       22   C4
   0       27   C5", header=TRUE)

df2 <- read.table(text = "
Time    Score   ID
 -83       19   C1
 -55       15   C3
   0       27   C5", header=TRUE)

I want to create a xyplot with df1, grouped by ID, but without labeling each ID. Then add points to the existing xyplot with points from df2

library(lattice)
xyplot(df1$Score ~ df1$Time | df1$ID, type ="b", cex = .5)

Would appreciate suggestions on how not to label each ID (remove panel title) then add df2 as points to the existing plot

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
user2783615
  • 829
  • 3
  • 11
  • 17

1 Answers1

2

Set strip=FALSE to suppress the 'panel titles'. Then, after taking care to make sure the ID factor levels in the two data.frames match up, you could use latticeExtra::as.layer() to plot data from the second data.frame onto the plot constructed using the first:

library(latticeExtra)

## Fiddly bit needed to make sure, e.g., that level `C3` is coded 
## using the same number (here a '3') in both ID columns.
df2$ID <- factor(df2$ID, levels = levels(df1$ID))

## Plot data from both data.frames onto the same plot
xyplot(Score ~ Time | ID, data = df1, type ="b", cex = .5, strip = FALSE) + 
as.layer(xyplot(Score ~ Time | ID, data = df2, type ="b", 
                cex = 5, drop.unused.levels = FALSE))

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455