用格子绘制 lme4 重新排序的青霉素数据

Plot lme4 reordered Penicillin data with lattice

我正在阅读 lme4 文档书籍以了解如何分析线性混合效应模型。我正在尝试重现所有步骤,但在尝试绘制重新排序的青霉素数据集时遇到问题,因为它出现在本网站的第 8.2.2 节多重随机效应 (http://www.john-ros.com/Rcourse/lme.html) Figure link: http://www.john-ros.com/Rcourse/Rcourse_files/figure-html/unnamed-chunk-181-1.png

到目前为止,我已经能够重新排序数据框并成功绘制出不同形状的点,但我无法 link 以正确的重新排序方式为每个样本绘制点。剧情是按原来的顺序来的,剧情看起来好乱。

我试图在网上寻找这个情节的代码,但我无法在任何地方找到它。如果这是一个微不足道的问题,我提前道歉。

这里有我到目前为止编写的代码:

library(lme4)
require(lattice)

myPCH <- 1:6
myLTY <- 1:6


# Plotting only the dots in the right way
xyplot(reorder(plate, diameter) ~ diameter, Penicillin,
       xlab="Diameter of growth inhibition zone (mm)",
       ylab = "Plate",
       groups=factor(sample,labels=as.character(unique(Penicillin$sample))),
       pch=myPCH, col=1:6,
       key =  list(columns=6,
                   text = list(as.character(unique(Penicillin$sample))),
                   points = list(pch=myPCH, col=1:6)),
       type = c("g","p"))


# Adding the lines in the wrong order (Modified so that they have 
# different styles
xyplot(reorder(plate, diameter) ~ diameter, data=Penicillin,
       xlab="Diameter of growth inhibition zone (mm)",
       ylab = "Plate",
       groups=factor(sample,labels=as.character(unique(Penicillin$sample))),
       pch=myPCH, col=1:6,
       lty=myLTY,
       key =  list(columns=6,
                   text = list(as.character(unique(Penicillin$sample))),
                   points = list(pch=myPCH, col=1:6)),
       type = c("g","p","l"))


# Trying to plot the dots and the lines separately with the 
# "panel = function(x, y, groups,...)" lattice function 

xyplot(reorder(plate, diameter) ~ diameter, data=Penicillin,
       groups=factor(sample,labels=as.character(unique(Penicillin$sample))),
       xlab="Diameter of growth inhibition zone (mm)",
       ylab = "Plate",
       key =  list(columns=6,
                   text = list(as.character(unique(Penicillin$sample))),
                   points = list(pch=myPCH, col=1:6)),
       panel = function(x, y, groups,...) {

       panel.xyplot(x, y,
                      pch=myPCH, col=1:6,
                      type = c("g","p"));

       panel.linejoin(x, y,
                        lty=myLTY, col=1:6,
                        type = c("g","l"))
       })


sessionInfo()

我想绘制不同形状的点和线,就像我之前提到的原始png图片中绘制的那样。

这是我的 sessionInfo():

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] MEMSS_0.9-2     lattice_0.20-38 ggplot2_3.1.0   lme4_1.1-19     Matrix_1.2-15  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       rstudioapi_0.9.0 magrittr_1.5     bindr_0.1.1      splines_3.5.1   
 [6] MASS_7.3-51.1    tidyselect_0.2.5 munsell_0.5.0    colorspace_1.4-0 R6_2.3.0        
[11] rlang_0.3.1      minqa_1.2.4      plyr_1.8.4       dplyr_0.7.8      tools_3.5.1     
[16] grid_3.5.1       gtable_0.2.0     nlme_3.1-137     withr_2.1.2      lazyeval_0.2.1  
[21] assertthat_0.2.0 tibble_2.0.1     crayon_1.3.4     bindrcpp_0.2.2   purrr_0.2.5     
[26] nloptr_1.2.1     glue_1.3.0       labeling_0.3     compiler_3.5.1   pillar_1.3.1    
[31] scales_1.0.0     pkgconfig_2.0.2

非常感谢您的帮助,

线条的连接顺序实际上取决于观察结果在数据中出现的顺序。为了最容易地得到这个数字,你只需要对数据进行排序。这里我复制一份并做排序

p2 <- transform(Penicillin, pd = reorder(plate, diameter))
p2 <- p2[order(p2$pd),]

然后我可以用它来绘制你创建的第一个代码块

xyplot(pd ~ diameter, data=p2, ...)

给出以下情节