Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot_model type="pred" with own title and solid not dashed error bars does not work #959

Open
bridgeovertroubledhuman opened this issue Jan 29, 2025 · 4 comments

Comments

@bridgeovertroubledhuman

I want a graph using plot_model with type "pred" with my own individual title and solid (not dashed) lines on the error bar. However, both don't work - any help highly appreciated.

My current MWE:

if (!require("pacman")) install.packages("pacman")
pacman::p_load( 'ggplot2', 'sjPlot')
update.packages(ask = FALSE)


data(efc)


efc$sexdummy <- NA
efc$sexdummy <- ifelse(efc$c161sex == 1, "Sex_1", "Sex_2")
efc$sexdummy<- as.factor(efc$sexdummy)
table(efc$sexdummy)



efc$carerrelation <- NA
efc$carerrelation <- ifelse(efc$e15relat == 1, "Child",
                            ifelse(efc$e15relat == 2, "Partner",
                                   "Other"))
efc$carerrelation<- as.factor(efc$carerrelation)
table(efc$carerrelation)

#My model
m2 <- glm(sexdummy ~ carerrelation  , data = efc, family = "binomial")


# The plot with title never works, gets me the error 
 plot_model(m2, type="pred",  title = "My title",
                       show.values = T, value.offset = .3, colors = "bw") 

# Only works without a title 
 plot_model(m2, type="pred", # title = "My title",
            show.values = T, value.offset = .3, colors = "bw") 

Thank you for fixing the issue.

For a picture see: https://stackoverflow.com/q/79396777/6473874?sem=2

Sadly, cannot repex it, as I always get this error:

reprex:::reprex_addin()
ℹ Rendering reprex...
Error in reprex_render() :
This reprex appears to crash R. Call reprex() again with std_out_err = TRUE to get more info.
ℹ Rendering reprex...
Error in reprex_render():
! This reprex appears to crash R. Call reprex() again with std_out_err = TRUE to get more info.
Run rlang::last_trace() to see where the error occurred.

@strengejacke
Copy link
Owner

Thanks for reporting. For now, as a workaround, you could add a title via ggplot:

library(sjPlot)
data(efc)

efc$sexdummy <- as.factor(ifelse(efc$c161sex == 1, "Sex_1", "Sex_2"))
efc$carerrelation <- ifelse(efc$e15relat == 1, "Child",
                            ifelse(efc$e15relat == 2, "Partner",
                                   "Other"))
efc$carerrelation<- as.factor(efc$carerrelation)

#My model
m2 <- glm(sexdummy ~ carerrelation  , data = efc, family = "binomial")

plot_model(
  m2,
  type = "pred",
  show.values = TRUE,
  value.offset = 0.3,
  colors = "bw"
) + ggplot2::ggtitle("My title")

Image

@bridgeovertroubledhuman
Copy link
Author

Thank you! While that works for the title, any chance in getting the error bars from dashed to solid? I tried that with ggplot but so far without any luck.

@strengejacke
Copy link
Owner

I suggest using the ggeffects package, which is internally used in sjPlot. user interface is very similar, but gives you more flexibility regarding plots:

library(ggeffects)
data(efc)

efc$sexdummy <- as.factor(ifelse(efc$c161sex == 1, "Sex_1", "Sex_2"))
efc$carerrelation <- ifelse(efc$e15relat == 1, "Child",
                            ifelse(efc$e15relat == 2, "Partner",
                                   "Other"))
efc$carerrelation<- as.factor(efc$carerrelation)

#My model
m2 <- glm(sexdummy ~ carerrelation  , data = efc, family = "binomial")


?plot.ggeffects
predict_response(m2, "carerrelation") |> plot()
predict_response(m2, "carerrelation") |> plot() + ggplot2::ggtitle("test")

See
https://strengejacke.github.io/ggeffects/articles/introduction_plotmethod.html
https://strengejacke.github.io/ggeffects/articles/introduction_plotcustomize.html

@bridgeovertroubledhuman
Copy link
Author

Oh, I See, I thought it was "just" a small bug in plot_model I had discovered. Thanks for the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants