## ----echo=FALSE---------------------------------------------------------------
req_suggested_packages <- c("emmeans", "ggplot2", "cowplot",
                            "ggbeeswarm", "ggthemes")
pcheck <- lapply(req_suggested_packages, requireNamespace, 
                 quietly = TRUE)
if (any(!unlist(pcheck))) {
   message("Required package(s) for this vignette are not available/installed and code will not be executed.")
   knitr::opts_chunk$set(eval = FALSE)
}

## ----set-options, echo=FALSE, cache=FALSE-----------------------------------------------
op <- options(width = 90)
knitr::opts_chunk$set(dpi=72)

## ----message=FALSE, warning=FALSE-------------------------------------------------------
library("afex")     
library("ggplot2")  
library("cowplot")
theme_set(theme_grey())

## ---------------------------------------------------------------------------------------
data(md_12.1)
(aw <- aov_ez("id", "rt", md_12.1, within = c("angle", "noise")))

## ----fig.width=7, fig.height=3.5, dpi = 100, out.width='90%'----------------------------
p_an <- afex_plot(aw, x = "angle", trace = "noise") 
p_na <- afex_plot(aw, x = "noise", trace = "angle")
plot_grid(p_an, p_na)  ## try adding: labels = "AUTO"

## ---------------------------------------------------------------------------------------
p_an <- afex_plot(aw, x = "angle", trace = "noise", error = "within",
                  factor_levels = list(angle = c("0°", "4°", "8°"),
                                    noise = c("Absent", "Present")), 
                  legend_title = "Noise") +
  labs(y = "RTs (in ms)", x = "Angle (in degrees)")

## ----fig.width=8.5, fig.height=6, dpi = 100, out.width='100%'---------------------------
plot_grid(
  p_an + theme_bw() + theme(legend.position="bottom"),
  p_an + theme_light() + theme(legend.position="bottom"),
  p_an + theme_minimal() + theme(legend.position="bottom"),
  p_an + jtools::theme_nice() + theme(legend.position="bottom"),
  p_an + ggpubr::theme_pubr(),
  p_an + theme_cowplot() + theme(legend.position="bottom"),
  labels = "AUTO"
)  

## ----fig.width=3.5, fig.height=3, dpi = 100, out.width='50%'----------------------------
p_an + 
  scale_y_continuous(breaks=seq(400, 900, length.out = 3)) +
  theme_bw(base_size = 15) + 
  theme(legend.position="bottom", 
        panel.grid.major.x = element_blank())

## ---------------------------------------------------------------------------------------
theme_set(theme_bw(base_size = 15) + 
            theme(legend.position="bottom", 
                  panel.grid.major.x = element_blank()))

## ----eval=FALSE-------------------------------------------------------------------------
# ggsave("my_plot.png", device = "png",
#        width = 9, height = 8, units = "cm",
#        dpi = 600) ## the larger the dpi, the better the resolution

## ----eval=FALSE-------------------------------------------------------------------------
# ggsave("my_plot.pdf", device = "pdf",
#        width = 9, height = 8, units = "cm")

## ----fig.width=7, fig.height=13, dpi = 100, out.width='100%'----------------------------
p0 <- afex_plot(aw, x = "noise", trace = "angle", error = "within")
p1 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.3,
                data_arg = list(
                  position = 
                    ggplot2::position_jitterdodge(
                      jitter.width = 0, 
                      jitter.height = 25, 
                      dodge.width = 0.3  ## needs to be same as dodge
                    )))
p2 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                data_geom = geom_count) +
  guides(size = "none")
p3 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", 
                data_geom = geom_violin, 
                data_arg = list(width = 0.5))
p4 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", 
                data_geom = geom_boxplot, 
                data_arg = list(width = 0.3, linetype = 1))
p5 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                data_geom = ggbeeswarm::geom_beeswarm,
                data_arg = list(
                  dodge.width = 0.5,  ## needs to be same as dodge
                  cex = 0.8))
p6 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                data_geom = ggbeeswarm::geom_quasirandom,
                data_arg = list(
                  dodge.width = 0.5,  ## needs to be same as dodge
                  cex = 0.8,
                  width = 0.05  ## choose small value so data points are not overlapping 
                ))
## currently deactivated 
# p7 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.7, 
#                 data_geom = ggpol::geom_boxjitter, 
#                 data_arg = list(
#                   width = 0.5, 
#                   jitter.params = list(width = 0, height = 10),
#                   outlier.intersect = TRUE),
#                 point_arg = list(size = 2.5), 
#                 error_arg = list(linewidth = 1.5, width = 0))
plot_grid(p0, p1, p2, p3, p4, p5, p6, ncol = 2, labels = 1:7)  

## ----fig.width=3.5, fig.height=3.5, dpi = 100, out.width='50%'--------------------------
afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
          data_geom = list(
            geom_violin, 
            ggbeeswarm::geom_quasirandom
          ),
          data_arg = list(
            list(draw_quantiles = c(0.25, 0.5, 0.75)),
            list(dodge.width = 0.5, width = 0.05)
          ))

## ----fig.width=8, fig.height=7, dpi = 100, out.width='100%'-----------------------------
p2 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                mapping = c("shape", "color"),
                data_geom = ggbeeswarm::geom_beeswarm,
                data_arg = list(
                  dodge.width = 0.5,  ## needs to be same as dodge
                  cex = 0.8))
p2b <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                mapping = c("shape", "color"),
                data_geom = ggbeeswarm::geom_beeswarm,
                data_arg = list(
                  colour = "darkgrey",
                  dodge.width = 0.5,  ## needs to be same as dodge
                  cex = 0.8)) +
  ggthemes::scale_color_colorblind()
p2c <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.5,
                mapping = c("shape", "color"),
                data_geom = ggbeeswarm::geom_beeswarm,
                data_arg = list(
                  dodge.width = 0.5,  ## needs to be same as dodge
                  cex = 0.8),
                point_arg = list(colour = "black"),
                line_arg = list(colour = "black"), 
                error_arg = list(colour = "black", width = 0)) +
  ggthemes::scale_color_colorblind()
p3 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", 
                mapping = c("linetype", "shape", "fill"),
                data_geom = ggplot2::geom_violin, 
                data_arg = list(width = 0.5))
# p5 <- afex_plot(aw, x = "noise", trace = "angle", error = "within", dodge = 0.7,
#                 mapping = c("shape", "fill"),
#                 data_geom = ggpol::geom_boxjitter, 
#                 data_arg = list(
#                   width = 0.5, 
#                   jitter.params = list(width = 0, height = 10),
#                   outlier.intersect = TRUE),
#                 point_arg = list(size = 2.5), 
#                 line_arg = list(linetype = 0),
#                 error_arg = list(linewidth = 1.5, width = 0))
plot_grid(p2, p2b, p2c, p3, ncol = 2) 

## ----fig.width=8.5, fig.height=4, dpi = 125, out.width='100%'---------------------------
p1 <- afex_plot(aw, x = "noise", trace = "angle", mapping = c("color"), 
                error = "within", 
                point_arg = list(size = 5), line_arg = list(size = 2),
                error_arg = list(linewidth = 2))
p2 <- afex_plot(aw, x = "noise", trace = "angle", 
                mapping = c("color", "shape", "linetype"), 
                error = "within", 
                point_arg = list(size = 5), line_arg = list(size = 2),
                error_arg = list(linewidth = 2, width = 0, linetype = 1))
plot_grid(p1, p2, ncol = 2)

## ----fig.width=7, fig.height=3.5, message=FALSE-----------------------------------------
po1 <- afex_plot(aw, x = "angle", mapping = "color", error = "within", 
                 point_arg = list(size = 2.5), 
                 error_arg = list(linewidth = 1.5, width = 0.05)) 
po2 <- afex_plot(aw, x = "angle", mapping = "color", error = "within", 
                 data_geom = geom_count,
                 point_arg = list(size = 2.5), 
                 error_arg = list(linewidth = 1.5, width = 0.05)) + 
  theme(legend.position="none")
plot_grid(po1, po2) 

## ----fig.width=7, fig.height=3.5, message=FALSE-----------------------------------------
afex_plot(aw, x = "angle", panel = "noise", error = "within",
          mapping = "color",
                 point_arg = list(size = 2.5), 
                 error_arg = list(linewidth = 1.5, width = 0.05)) + 
  theme(legend.position="none")

## ----fig.width=7, fig.height=3.5, message=FALSE-----------------------------------------
pn1 <- po1 + geom_line(aes(group = 1), color = "darkgrey", linewidth = 1.5)
pn2 <- afex_plot(
  aw, x = "angle", mapping = "color", error = "within", 
  point_arg = list(size = 2.5), 
  error_arg = list(linewidth = 1.5, width = 0.05),
  plot_first = geom_line(aes(group = 1), color = "darkgrey", linewidth = 1.5)) 
plot_grid(pn1, pn2) 

## ----fig.width=7, fig.height=3.5, message=FALSE-----------------------------------------
ps1 <- afex_plot(aw, x = "angle", error = "within", 
          data_geom = list(geom_count, geom_line),
          data_arg = list(list(), list(aes(group = id)))) +
  theme(legend.position="none")
ps2 <- afex_plot(aw, x = "angle", error = "within", 
                 mapping = c("shape", "colour"),
          data_geom = list(geom_count, geom_line),
          data_arg = list(list(colour = "darkgrey", shape = 19), 
                          list(aes(group = id), colour = "darkgrey")),
          point_arg = list(size = 2.5), 
          error_arg = list(linewidth = 1.0, width = 0.05),
          plot_first = geom_line(aes(group = 1), colour = "black")) +
  theme(legend.position="none") +
  ggthemes::scale_color_colorblind()
plot_grid(ps1, ps2)

## ----echo=FALSE-------------------------------------------------------------------------
load(system.file("extdata/", "output_afex_plot_mixed_vignette_model.rda", package = "afex"))

## ----eval=FALSE-------------------------------------------------------------------------
# data("fhch2010") # load
# fhch <- droplevels(fhch2010[ fhch2010$correct,]) # remove errors
# m9s <- mixed(log_rt ~ task*stimulus*density*frequency +
#                (stimulus+frequency||id)+
#                (task|item), fhch, method = "S",
#              control = lmerControl(optCtrl = list(maxfun = 1e6)),
#              expand_re = TRUE)

## ---------------------------------------------------------------------------------------
emmeans::emm_options(lmer.df = "asymptotic")

## ---------------------------------------------------------------------------------------
m9s

## ----fig.width=7, fig.height=3.5--------------------------------------------------------
afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task") 

## ----fig.width=7, fig.height=3.5--------------------------------------------------------
plot_grid( 
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "id"), 
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "item"), 
  labels = c("ID", "Item") 
)

## ----fig.width=7, fig.height=3.5--------------------------------------------------------
plot_grid( 
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "item", dodge = 0.8,
            data_geom = geom_violin, 
            data_arg = list(width = 0.5)), 
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "item", dodge = 0.8,
            data_geom = geom_boxplot, 
            data_arg = list(width = 0.5),
            error_arg = list(linewidth = 1.5, width = 0, linetype = 1))
)

## ----eval=FALSE-------------------------------------------------------------------------
# pairs(emmeans::emmeans(mrt, c("stimulus", "frequency"), by = "task"))

## ----echo=FALSE-------------------------------------------------------------------------
cat(aout_2$output, sep = "\n")

## ----fig.width=7, fig.height=3.5--------------------------------------------------------
plot_grid( 
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "id", error = "within"),
  afex_plot(m9s, x = "stimulus", trace = "frequency", panel = "task", 
            id = "item", dodge = 0.8, error = "within",
            data_geom = geom_violin, 
            data_arg = list(width = 0.5))
)

## ----include=FALSE------------------------------------------------------------
options(op)

