Search code examples
rscatter-plotmetafor

How can I overlap 2 scatterplots with with x=mean age and y = Risk-taking?


I want to make 2 scatterplots about risk-taking and sexes. I have 12 studies, each has data (means, effect sizes, sampling variance) for males and females. The means in each study are very different, in one study it is 1490 for males and 1200 for females, in another one the mean is 33 for males and 25 for females. I want to make 2 different scatterplots that are on the same graph. The X-axis should be age, the Y-axis should be Risk-taking. I need 2 different curves, one for females and 1 for males. How can I manage to fuse those 2 curves? And is it even possible to get everything on one graph?

I've tried things with ggplot2, geom_point(), and the metafor package.

# 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")

I expect to being able to fuse my 2 scatterplots into one, but I don't know how. Also, the 2 scatterplots look very similar to each other, which I think should not be.


Solution

  • I think plotting the second plot on a secondary axis should solve your problem. Add new = T as a parameter on your second par() call and the second plot will be drawn on top of the first one.

    Then run axis(side=4) on the end of your code to produce the axis for the second plot on the right side of the graph