1

I'm trying to add the datapoints using geom_point() to the plot_model() that is showing the linear relationship between the age and left.LGN variables.

plot <- plot_model(m1, terms = c("age"), type = "pred") +
    geom_point(data = data, aes(x = age, y = Left.LGN)) +
    labs(x = "Age (standardized)", y = "Adj. Left-LGN (standardized volume)", size = 20)

The script works until I add the third variable ("sex"):

plot <- plot_model(m1, terms = c("age", "sex"), type = "pred") +
   geom_point(data = data, aes(x = age, y = Left.LGN, color = sex, group = sex)) +
   labs(x = "Age (standardized)", y = "Adj. Left-LGN (standardized volume)", size = 20)

in which I got this error:

Error in `geom_point()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'group_col' not found
Run `rlang::last_error()` to see where the error occurred.

Has anybody had the same issue?

I expect to have a plot showing the relationship between age and Left-LGN volume related to males and females, adding the datapoints from the data.

0

1 Answer 1

2

We don't have your data or model, but we can reproduce your problem with the following set-up:

library(sjPlot)
library(ggplot2)

set.seed(1)

data <- data.frame(age = rep(31:70, 2),
                   Left.LGN = c(31:70, 41:80)/6 + 100 + rnorm(80),
                   sex = rep(c('Male', 'Female'), each = 40))

m1 <- lm(Left.LGN ~ age + sex, data)

Now there is no problem using your own plotting code to generate a plot with just age:

plot_model(m1, terms = c("age"), type = "pred") +
  geom_point(data = data, aes(x = age, y = Left.LGN)) +
  labs(x = "Age (standardized)", 
       y = "Adj. Left-LGN (standardized volume)", 
       size = 20)

But if we try to add in the sex variable, we get the same error that you report:

plot_model(m1, terms = c("age", "sex"), type = "pred") +
  geom_point(data = data, 
             aes(x = age, y = Left.LGN, color = sex, group = sex)) +
  labs(x = "Age (standardized)", 
       y = "Adj. Left-LGN (standardized volume)", 
       size = 20)
#> Error in `geom_point()`:
#> ! Problem while computing aesthetics.
#> i Error occurred in the 3rd layer.
#> Caused by error in `FUN()`:
#> ! object 'group_col' not found

The reason for the error is that plot_model generates a ggplot object, which has aesthetic mappings to variables present in the data frame that plot_model creates from your model. One of these variables is called group_col, which of course is not present in your own data frame. To fix this, simply specify inherit.aes = FALSE in your geom_point layer:

plot_model(m1, terms = c("age", "sex"), type = "pred") +
  geom_point(data = data, 
             aes(x = age, y = Left.LGN, color = sex, group = sex),
             inherit.aes = FALSE) +
  labs(x = "Age (standardized)", 
       y = "Adj. Left-LGN (standardized volume)", 
       size = 20)

Note though that plot_model also includes the parameter show.data if you want to include the data points in your model, so instead of adding a geom_point layer, you can do:

plot_model(m1, terms = c("age", "sex"), type = "pred", show.data = TRUE) +
  labs(x = "Age (standardized)", 
       y = "Adj. Left-LGN (standardized volume)", 
       size = 20)

enter image description here

Created on 2023-09-14 with reprex v2.0.2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.