I have difficulties with the following task: I want to plot the predicted effect of a continuous variable in a binary logistic regression analysis. This is easy. However, if I want plot the effect of two variables on the same dependent, I can only do this next to each other, but I want to have it overlapping.
To demonstrate this, I use this example data on sex and weight/height (of course there is no rational behind this).
# dataframe with two groups (males/females) and respective weight and height
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("0", "1"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5))),
height=round(c(rnorm(200, mean=160, sd=10),
rnorm(200, mean=170, sd=10)))
)
library(ggplot2)
library(sjPlot)
library(ggpubr)
lm1 <- glm(sex ~ weight, data=df, family="binomial")
lm2 <- glm(sex ~ height, data=df, family="binomial")
# Current version: Plot them next to each other.
p1 <- plot_model(lm1, type="pred", term="weight")
p2 <- plot_model(lm2, type="pred", term="height")
ggarrange(p1,p2)
# Desired version: Plot them overlapping over each other (potentially without confidence intervals)
As arthur.t showed, facetting is better in this case. However, in case you do need to lay out two separate plots together in the future, you can use the ncol or nrow argument in ggarrange. For example:
ggarrange(p1, p2, ncol=1)
I generally use the patchwork package for laying out multiple plots. In that case, the code would be:
Thank you for your help!
Although this is useful, and some might say that placing them next to each other is "better", I would need/like to both add them in one graph - like we often do for AUC/ROC curves:
Thank you for your help!
Although this is useful, and some might say that placing them next to each other is "better", I would need/like to both add them in one graph - like we often do for AUC/ROC curves:
However, I would like them "overlapping" --> let's assume the x-axis will be the same, and I have two separate logistic regressions with independent outcomes (sex in regression 1 and sex in regression 2)