5

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

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)

问题: 我想在 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)}
)

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

任何想法都非常受欢迎!

非常感谢。

4

1 回答 1

4

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

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

panel=function(...){
              panel.dotplot(...)
              panel.abline(v=0.25)
              panel.abline(v=-0.25)
              }

完整代码:

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)

在此处输入图像描述

于 2012-09-03T11:31:36.587 回答