如何将2个散点图与x =平均年龄和y =冒险相重叠?

时间:2019-02-08 10:07:27

标签: r scatter-plot metafor

我想就冒险和性别做2个散点图。我有12个研究,每个研究都有男性和女性的数据(均值,效应大小,抽样方差)。每项研究的均值差异很大,在一项研究中,男性为1490,女性为1200,在另一项研究中,男性为33,女性为25。我想在同一张图上进行2个不同的散点图绘制。 X轴应为年龄,Y轴应为冒险。我需要2条不同的曲线,一条用于女性,另一条用于男性。我该如何融合这两条曲线?甚至有可能将所有内容放在一张图上吗?

我已经尝试过使用ggplot2,geom_point()和metafor软件包。

# yi = effect sizes of each study
# vi = sampling variance 
# data = mydata
library("metafor") 


# adjust margins so the space is better used
par(mar=c(5,5,1,2))

# fit mixed-effects model with age as predictor
res <- rma(yi, vi, mods = ~ magewomen, data=mydata)

# calculate predicted risk ratios for womens’ age 0-30.
preds <- predict(res, newmods=c(0:35), transf=exp)

# calculate point sizes by rescaling the standard errors
wi    <- 1/sqrt(mydata$vi)
size  <- 0.5 + 3.0 * (wi - min(wi))/(max(wi) - min(wi))

# plot the risk ratios against women’s age
women <- plot(mydata$magewomen, exp(mydata$yi), pch=19, cex=size, 
     xlab="womens age", ylab="Risk",
     las=1, bty="l", log="y")

# add predicted values (and corresponding CI bounds)
lines(0:35, preds$pred)
lines(0:35, preds$ci.lb, lty="dashed")
lines(0:35, preds$ci.ub, lty="dashed")

# Same procedure, just for men 

# adjust margins so the space is better used
par(mar=c(5,5,1,2))

# fit mixed-effects model with men’s age as predictor
res2 <- rma(yi, vi, mods = ~ magemen, data=mydata)

# calculate predicted risk ratios for men’s age from 0-30
preds2 <- predict(res2, newmods=c(0:35), transf=exp)

# calculate point sizes by rescaling the standard errors
wi    <- 1/sqrt(mydata$vi)
size  <- 0.5 + 3.0 * (wi - min(wi))/(max(wi) - min(wi))

# plot the risk ratios against men’s age
men <- plot(mydata$magemen, exp(mydata$yi), pch=19, cex=size, 
     xlab="mens age", ylab="Risk",
     las=1, bty="l", log="y")

# add predicted values (and corresponding CI bounds)
lines(0:35, preds$pred)
lines(0:35, preds$ci.lb, lty="dashed")
lines(0:35, preds$ci.ub, lty="dashed")

我希望能够将2个散点图融合为一个,但我不知道如何。另外,这两个散点图看起来非常相似,我认为不应该如此。

1 个答案:

答案 0 :(得分:0)

我认为在辅助轴上绘制第二个图应该可以解决您的问题。在第二个new = T调用中添加par()作为参数,第二个图将绘制在第一个图的顶部。

然后在代码末尾运行axis(side=4),以在图形的右侧生成第二个绘图的轴