rggplot2plotsjplot

R: sjplot: plot_model type "pred" adding a title and making error lines solid


I want the graph using plot_model with a unique title "My title" and with 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
 plot_model(m2, type="pred",  title = "My title",
                       show.values = T, value.offset = .3, colors = "bw") 

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

And the graph I get (left), as well as the graph I want (on the right):

graph I get on the left, graph I want (own title, solid lines) on the right

The relevant error I get:

>  plot_model(m2, type="pred",  title = "My title",
+                        show.values = T, value.offset = .3, colors = "bw") 
Error in `purrr::map()`:
ℹ In index: 1.
ℹ With name: data.
Caused by error in `FUN()`:
! non-numeric argument to binary operator
Run `rlang::last_trace()` to see where the error occurred.

My session info

sessionInfo( ) R version 4.4.0 (2024-04-24) Platform: aarch64-apple-darwin20 Running under: macOS Sonoma 14.0

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib

LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;

LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sjPlot_2.8.17 ggplot2_3.5.1 pacman_0.5.1 

loaded via a namespace (and not attached):
 [1] gtable_0.3.6       dplyr_1.1.4        compiler_4.4.0     tidyselect_1.2.1   stringr_1.5.1      snakecase_0.11.1  
 [7] tidyr_1.3.1        scales_1.3.0       R6_2.5.1           labeling_0.4.3     generics_0.1.3     sjlabelled_1.2.0  
[13] knitr_1.49         forcats_1.0.0      tibble_3.2.1       insight_1.0.1      munsell_0.5.1      pillar_1.10.1     
[19] sjstats_0.19.0     rlang_1.1.5        performance_0.13.0 stringi_1.8.4      xfun_0.50          ggeffects_2.1.0   
[25] datawizard_1.0.0   cli_3.6.3          withr_3.0.2        magrittr_2.0.3     grid_4.4.0         rstudioapi_0.17.1 
[31] haven_2.5.4        hms_1.1.3          lifecycle_1.0.4    sjmisc_2.8.10      vctrs_0.6.5        evaluate_1.0.3    
[37] glue_1.8.0         farver_2.1.2       colorspace_2.1-1   purrr_1.0.2        tools_4.4.0        pkgconfig_2.0.3

Solution

  • The author of the package answered this here: https://github.com/strengejacke/sjPlot/issues/959#issuecomment-2621728814

    Therefore, use + ggplot2::ggtitle("My title") for the title and use ggeeffects for the solid lines.