-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a1572b
commit daf292a
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
``` | ||
|