首页 > 解决方案 > 以 Lattice 中的两个变量为条件

问题描述

这是我正在使用的数据框:

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")

我想按他们所属的组对个人(id)进行排序。

当我写:

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

我为每个组得到了一行散点图,但每一行都有与该组无关的个人的空白单元格。我想摆脱这些空白单元格。

感谢您的时间和关注。

R 版本 3.5.2 (2018-12-20) 平台:x86_64-w64-mingw32/x64 (64-bit) 运行条件:Windows >= 8 x64 (build 9200) 附加基础包:[1] stats graphics grDevices utils datasets方法基于
其他附加包:[1] lattice_0.20-38 通过命名空间加载(未附加):[1] compiler_3.5.2 tools_3.5.2 grid_3.5.2

标签: rlattice

解决方案


我认为您要问的实际上是不可能的(请参阅此处https://stat.ethz.ch/pipermail/r-help/2004-September/057160.html)。一种可能的解决方案是使用模型公式v6 ~ time |group:id而不是v6 ~ time |group+id,如上面的链接中所示,但这只会留下一个带有 group by id 交互的条带。

否则,下面的代码会产生一个接近我认为你想要的情节,但它非常基于黑客:

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)

在此处输入图像描述

否则,如果使用 ggplot2 是一个选项:

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") 

推荐阅读