使用dotplot可视化前/后匹配平衡统计

TiF*_*TiF 5 r graph matching lattice

为了在匹配过程之前和之后直观地显示协变量平衡,我编写了以下代码:

library(lattice)
library(gridExtra)

StandBias.df= data.frame(
  Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
  Groups = factor(c(rep("0", 6), rep("1", 6))),
  Values = c(0.26, 0.4, 0.3, 0.21, 0.6, 0.14, 0.12, -0.04, -0.23, 0.08, 0.14, -0.27))

d1<-dotplot(Category ~Values, data = StandBias.df, groups = Groups,
            main = "Standardized Mean Differences", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
            key=list(text=list(c("Pre-Matching", "Post-Matching")),
                     points=list(col = c("black", "grey50"), pch=c(22,15)), 
                     space="bottom", border=T))

Ttest.df = data.frame(
  Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
  Groups = factor(c(rep("0", 6), rep("1", 6))),
  Values = c(0.12, 0.02, 0.69, 0.19, 0.05, 0.01, 0.62, 0.77, 0.54, 0.24, 0.92, 0.51))


d2<-dotplot(Category ~Values, data = Ttest.df, groups = Groups,
            main = "p-values", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
            key=list(text=list(c("Pre-Matching", "Post-Matching")),
                     points=list(col = c("black", "grey50"), pch=c(22,15)), 
                     space="bottom", border=T))

grid.arrange(d1,d2,nrow=1)
Run Code Online (Sandbox Code Playgroud)

问题: 我想在0.1处为p值(d2)和[-0.25; 0.25]对于标准化的平均值(d1),以便我们有平衡/不平衡的视觉截止.

这就是我尝试的:对于d1,我在最后一行之后添加了以下行,即

...

space ="bottom", border=T),

panel=function(...){
    panel.abline(v=0.25)
    panel.abline(v=-0.25)}
)
Run Code Online (Sandbox Code Playgroud)

修改后的代码生成带有请求的垂直线但没有数据点的图.

任何想法都非常欢迎!

非常感谢.

And*_*rie 4

你快到了。编写自定义面板时,您需要包含原始面板代码,否则不会绘制任何内容。

因此,面板函数应如下所示(添加panel.dotplot(...)到您的代码中):

panel=function(...){
              panel.dotplot(...)
              panel.abline(v=0.25)
              panel.abline(v=-0.25)
              }
Run Code Online (Sandbox Code Playgroud)

完整代码:

d1<-dotplot(Category ~Values, data = StandBias.df, groups = Groups,
            main = "Standardized Mean Differences", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
            key=list(text=list(c("Pre-Matching", "Post-Matching")),
                     points=list(col = c("black", "grey50"), pch=c(22,15)), 
                     space="bottom", border=T),
            panel=function(...){
              panel.dotplot(...)
              panel.abline(v=0.25)
              panel.abline(v=-0.25)
              }
)

Ttest.df = data.frame(
  Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
  Groups = factor(c(rep("0", 6), rep("1", 6))),
  Values = c(0.12, 0.02, 0.69, 0.19, 0.05, 0.01, 0.62, 0.77, 0.54, 0.24, 0.92, 0.51))


d2<-dotplot(Category ~Values, data = Ttest.df, groups = Groups,
            main = "p-values", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
            key=list(text=list(c("Pre-Matching", "Post-Matching")),
                     points=list(col = c("black", "grey50"), pch=c(22,15)), 
                     space="bottom", border=T),
          panel=function(...){
            panel.dotplot(...)
            panel.abline(v=0.25)
            panel.abline(v=-0.25)
          }
)

grid.arrange(d1,d2,nrow=1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述