Search code examples
rr-markdownr-exams

Why does `exams` prints the figure legend when plotting `ggplot2` plots?


When I use exams2nops() or exams2moodle()

Question
========
Some questions...
pacman::p_load(ggplot2, magrittr)
#make sample data
groups <- letters[23:26]

df <- data.frame(x = rnorm(n = 60,mean = 3.5, sd = 3.4),
                 y = rnorm(n = 60,mean = 3.5, sd = 3.4),
                 g = c(rep(groups[1],15),rep(groups[2],15),rep(groups[3],15),rep(groups[4],15)))


colors_pal <- c("#264653", "#2a9d8f", "#457b9d", "#e76f51")

plot_reg_line_equation <- function(df, groups, colors_pal) {
    ggplot(data = dplyr::filter(.data = df, g==groups), aes(x = x, y = y)) +
    geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
    geom_point() + 
    ggtitle(label = paste0(groups)) +
    jtools::theme_apa() +
    theme(axis.line = element_line(colour = "black"),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank())
  }

lapply(groups, plot_reg_line_equation, df = df) %>% patchwork::wrap_plots()
solution <- groups==groups[1]
exams::answerlist(groups, markup = "markdown")
Solution
========
Some solution feedback.


Meta-information
================
exname: example
extype: schoice
exsolution: `r paste(solution)`
exshuffle: 4

I end up with two strange things, first the legend. And second the answer options.

enter image description here

Note: I noticed that if I had some kind of text immediatly aftwer the plot chunk it does not produced the legend (e.g. even if it is html <br>) however, I would like to understand this behavior.


Solution

  • Simply set fig.cap = "" in the options for the R code chunk that generates the figure. As usual in R/Markdown this suppresses the caption of the figure.

    Further notes:

    • You haven't shown the options that you use currently.
    • In the exsolution you need mchoice2string() instead of paste().