Arrange two ggplot-objects over each other

Dear all,

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)

Thank you for your help!

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)
library(tidyverse)
library(modelr)

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)

df %>% 
  add_predictions(lm1, "pred_weight", type = "response") %>% 
  add_predictions(lm2, "pred_height", type = "response") %>%
  pivot_longer(c(height, weight)) %>%
  mutate(pred = if_else(name == "height", pred_height, pred_weight)) %>%
  ggplot() + 
  aes(value, pred, color = name) + 
  geom_line() +
  facet_wrap(~name, ncol = 1, scales = "free")

Created on 2021-08-12 by the reprex package (v1.0.0)

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:

library(patchwork)

p1 / p2

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:

I would like both to be on the same graph. Is this possible?

Thank you for your help!

Yours,
Georg

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:

I would like both to be on the same graph. Is this possible?

Thank you for your help!

Yours,
Georg

Yes, although I don't think it's a great idea because height and weight have different units.

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(tidyverse)
library(modelr)

lm1  <- glm(sex ~ weight, data=df, family="binomial")
lm2  <- glm(sex ~ height, data=df, family="binomial")

df %>% 
  add_predictions(lm1, "pred_weight", type = "response") %>% 
  add_predictions(lm2, "pred_height", type = "response") %>%
  pivot_longer(c(height, weight)) %>%
  mutate(pred = if_else(name == "height", pred_height, pred_weight)) %>%
  ggplot() + 
  aes(value, pred, color = name) + 
  geom_line() #+

  #facet_wrap(~name, ncol = 1, scales = "free")

Created on 2021-08-12 by the reprex package (v1.0.0)

Thank you so much again.

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)

set.seed(1234)
df1 <- 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)))
)

df2 <- data.frame(
  sex=factor(rep(c("0", "1"), each=200)),
  weight=round(c(rnorm(200, mean=45, sd=5),
                 rnorm(200, mean=75, sd=5)))
)

library(tidyverse)
library(modelr)

lm1  <- glm(sex ~ weight, data=df1, family="binomial")
lm2  <- glm(sex ~ weight, data=df2, family="binomial")

Is this possible?

Similar to this (but here they used groups abc within the same logistic regression)

Thank you a final time!

set.seed(1234)
df1 <- 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)))
)

df2 <- data.frame(
  sex=factor(rep(c("0", "1"), each=200)),
  weight=round(c(rnorm(200, mean=45, sd=5),
                 rnorm(200, mean=75, sd=5)))
)

library(tidyverse)
library(modelr)

lm1  <- glm(sex ~ weight, data=df1, family="binomial")
lm2  <- glm(sex ~ weight, data=df2, family="binomial")

df1 <- add_predictions(df1,
                       lm1, 
                       "pred_weight", 
                       type = "response")
df2 <- add_predictions(df2,
                       lm2, 
                       "pred_weight", 
                       type = "response")

df_12 <- bind_rows(df1,df2,.id = "name")

  ggplot(data = df_12) + 
  aes(weight, pred_weight, color = name) + 
  geom_line()

Perfect, thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.