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

时间:2012-09-03 11:11:01

标签: 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)

问题: 我想在0.1处为p值(d2)和[-0.25;添加垂直线。对于标准化的平均值(d1),我们有一个平衡/不平衡的视觉截止值。

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

...

space ="bottom", border=T),

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

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

非常欢迎任何想法!

非常感谢。

1 个答案:

答案 0 :(得分: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)

enter image description here