strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
556 stars 35 forks source link

'Tweak' arguments of `plot.ggeffects()` not working #386

Closed anhsmith closed 1 year ago

anhsmith commented 1 year ago

When I try to use the arguments show_ci and show_data, when applying the plot() function to a ggeffects object, I get an error.

library(ggeffects)

data(iris)

m1 <- lm(Sepal.Length ~ Species, data = iris)

m1_ggp <- ggpredict(m1, terms = "Species")

# this works
plot(m1_ggp)

# these don't
plot(m1_ggp, show_ci = F)
plot(m1_ggp, show_data = T)

The last two commands give the following error:

Error in ggplot2::scale_y_continuous(...) : 
  unused argument (show_ci = FALSE)

Likewise for an ANCOVA model:

m2 <- lm(Sepal.Length ~ Species + Sepal.Width, data = iris)

m2_gge <- ggpredict(m2, terms = c("Species", "Sepal.Width"))

plot(m2_gge, show_ci = F)

And an example from a ggeffects vignette gave the same error:

library(ggeffects)
library(sjmisc)

data(efc)

efc$c172code <- to_label(efc$c172code)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)

dat <- ggpredict(fit, terms = c("c12hour", "c172code"))
plot(dat, show_data = TRUE)

I'm aware that I can use ggplot() and create a customised plot.

> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_New Zealand.utf8  LC_CTYPE=English_New Zealand.utf8   
[3] LC_MONETARY=English_New Zealand.utf8 LC_NUMERIC=C                        
[5] LC_TIME=English_New Zealand.utf8    

time zone: Pacific/Auckland
tzcode source: internal

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

other attached packages:
 [1] sjmisc_2.8.9    lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.2    
 [6] purrr_1.0.2     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.3  
[11] tidyverse_2.0.0 ggeffects_1.3.1

loaded via a namespace (and not attached):
 [1] utf8_1.2.3         generics_0.1.3     stringi_1.7.12     lattice_0.21-8    
 [5] lme4_1.1-34        hms_1.1.3          magrittr_2.0.3     timechange_0.2.0  
 [9] grid_4.3.1         estimability_1.4.1 effects_4.2-2      Matrix_1.6-1      
[13] nnet_7.3-19        DBI_1.1.3          survival_3.5-5     fansi_1.0.4       
[17] scales_1.2.1       cli_3.6.1          mitools_2.4        rlang_1.1.1       
[21] munsell_0.5.0      splines_4.3.1      withr_2.5.0        tools_4.3.1       
[25] datawizard_0.8.0   tzdb_0.4.0         nloptr_2.0.3       minqa_1.2.5       
[29] colorspace_2.1-0   sjlabelled_1.2.0   boot_1.3-28.1      vctrs_0.6.3       
[33] R6_2.5.1           lifecycle_1.0.3    snakecase_0.11.1   MASS_7.3-60       
[37] insight_0.19.3     pkgconfig_2.0.3    pillar_1.9.0       gtable_0.3.4      
[41] glue_1.6.2         Rcpp_1.0.11        haven_2.5.3        tidyselect_1.2.0  
[45] rstudioapi_0.15.0  farver_2.1.1       nlme_3.1-163       survey_4.2-1      
[49] carData_3.0-5      labeling_0.4.2     compiler_4.3.1    
strengejacke commented 1 year ago

These arguments are available in the current development version, for the current CRAN version, the old argument names still apply (i.e. ci instead of show_ci and rawdata resp. add.data for show_data). Either use the "old" argument names, or update to the latest development version using ggeffects::install_latest():

library(ggeffects)
data(iris)
m1 <- lm(Sepal.Length ~ Species, data = iris)

m1_ggp <- ggpredict(m1, terms = "Species")

plot(m1_ggp, show_ci = FALSE)

plot(m1_ggp, show_data = TRUE)
#> Data points may overlap. Use the `jitter` argument to add some amount of
#>   random variation to the location of data points and avoid overplotting.

Created on 2023-10-05 with reprex v2.0.2