1
votes

This is the data frame I'm working with:

datal2<- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("49180", "75725", 
"88743", "172231"), class = "factor"), group = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Control", 
"Intervention"), class = "factor"), time = c(1L, 2L, 3L, 4L, 
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), v6 = c(0.2, 
0.27, NA, NA, 0.36, 0.4, 0.35, 0.36, 0.44, 0.57, 0.65, 0.59, 
0.67, 0.86, 0.86, 0.78)), reshapeLong = list(varying = structure(list(
v6 = c("v6_Ing", "v6_Alt", "v6_M1", "v6_M3")), v.names = "v6", times = 1:4), 
v.names = "v6", idvar = "id", timevar = "time"), row.names = c("49180.1", 
"49180.2", "49180.3", "49180.4", "75725.1", "75725.2", "75725.3", 
"75725.4", "88743.1", "88743.2", "88743.3", "88743.4", "172231.1", 
"172231.2", "172231.3", "172231.4"), class = "data.frame")

I want to order the individuals (id) by the group they belong to.

When I write:

library(lattice)
print(xyplot(v6 ~ time |id+group, groups=group, datal2, aspect = "xy",
         type = c("g", "p", "r"),as.table=TRUE,auto.key=TRUE,
         xlab = "Measurement times",
         ylab = "Gait speed",
         scales = list(cex = 1,
                       x = list(tick.number = 4))))

I got one row of scatter plots for each group, but each row has blank cells for individuals that do not pertain to that group. I would like to get rid of these blank cells.

Thank you for your time and attention.

R version 3.5.2 (2018-12-20) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200) attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] lattice_0.20-38 loaded via a namespace (and not attached): [1] compiler_3.5.2 tools_3.5.2 grid_3.5.2

1

1 Answers

1
votes

I think what you're asking is not really possible (see here https://stat.ethz.ch/pipermail/r-help/2004-September/057160.html ). A possible solution is to use the model formula v6 ~ time |group:id instead of v6 ~ time |group+id, as indicated in the link above, but this will only leave a single strip with the group by id interaction.

Otherwise the code below produces a plot close to what I think you'd like, but it is very much based on a hack:

datal2$id2 = rep(c("A", "B"), each=8) #add a fake factor

## use a custom strip function to rename "ad hoc" the levels of the fake factor
customstrip = function(which.given, which.panel, ..., factor.levels){

    levs = if (which.given==1){
               print(which.panel)
               if ((which.panel[1] == 1) & (which.panel[2] == 1)){
                   c("49180", "49180")
               } else if ((which.panel[1] == 1) & (which.panel[2] == 2)){
                   c("75725", "75725")
               } else if ((which.panel[1] == 2) & (which.panel[2] == 1)){
                   c("172231", "172231")
               } else {
                   c("88743", "88743")
               }
           } else if  (which.given==2){
               c("Control", "Intervention")
           }
    strip.default(which.given, which.panel, ..., factor.levels = levs)
}

p2 = xyplot(v6 ~ time |id2+group, groups=group, datal2, aspect = "xy",
         type = c("g", "p", "r"), as.table=TRUE, auto.key=TRUE,
         xlab = "Measurement times",
         ylab = "Gait speed",
         scales = list(cex = 1,
                       x = list(tick.number = 4)),
         layout=c(2,2,1),
         strip=customstrip)
print(p2)

enter image description here

Otherwise if using ggplot2 is an option:

library(ggplot2)
p = ggplot(datal2, aes(time, v6, color=group)) + geom_point()
p = p + facet_wrap(~group+id, ncol=2) + theme_bw()
p = p + geom_smooth(method="lm", se=F) 
p = p + ylab("Gait speed") + xlab("Measurement times") 
p = p + scale_color_discrete(name="Group")