From daf292afc5b0687e136ae19f66acef7a000dd6d9 Mon Sep 17 00:00:00 2001 From: Mitchell O'Hara-Wild Date: Thu, 16 Jan 2025 19:28:02 +1100 Subject: [PATCH] Move rmd example into man/examples --- README.Rmd | 2 +- man/examples/draw-scatterplots.Rmd | 59 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 man/examples/draw-scatterplots.Rmd diff --git a/README.Rmd b/README.Rmd index 523acb5..4d38f11 100644 --- a/README.Rmd +++ b/README.Rmd @@ -41,7 +41,7 @@ The above quiz is created from the R Markdown document below. Knitting the docum ```{r, echo = FALSE, results="asis"} cat("````\n") -cat(readLines("inst/examples/draw-scatterplots.Rmd"), sep = "\n") +cat(readLines("man/examples/draw-scatterplots.Rmd"), sep = "\n") cat("````") ``` diff --git a/man/examples/draw-scatterplots.Rmd b/man/examples/draw-scatterplots.Rmd new file mode 100644 index 0000000..debe83d --- /dev/null +++ b/man/examples/draw-scatterplots.Rmd @@ -0,0 +1,59 @@ +--- +title: Drawing a scatterplot +output: + moodlequiz::moodlequiz: + replicates: 5 +moodlequiz: + category: datavis-scatterplots +--- + +# Data + +```{r setup, include = FALSE} +library(tidyverse) +library(rlang) +library(moodlequiz) +knitr::opts_chunk$set(echo = FALSE, + results = "hide", + fig.height = 4, + fig.width = 5, + fig.path = "", + fig.cap = "", + fig.align = "center") +``` + +```{r data} +cols <- colnames(mtcars) +cats <- c("cyl", "vs", "am", "gear", "carb") +nums <- setdiff(cols, cats) +x <- sample(nums, 1) +y <- sample(setdiff(nums, x), 1) +color <- sample(cats, 1) +size <- sample(setdiff(nums, c(x, y)), 1) +``` + +You have been asked to analyse the `mtcars` data. The variables and the class types of the data is shown below. + +```{r, echo = TRUE, results = "show"} +str(mtcars) +``` + +# Plot + +As a starting point, you decide to draw a scatter plot for some variables. Complete the code below to get the target plot below: + +```r +ggplot(mtcars, aes(x = `r cloze(x, cols)`, + y = `r cloze(y, cols)`, + color = factor(`r cloze(color, cols)`), + size = `r cloze(size, cols)`)) + + `r cloze("geom_point", ls(envir = as.environment("package:ggplot2"), pattern = "^geom_"))`() +``` + + +```{r, results = "show"} +ggplot(mtcars, aes(!!sym(x), !!sym(y), size = !!sym(size), color = factor(!!sym(color)))) + + geom_point() + + theme(plot.background = element_rect(color = "black")) +``` +