-
Notifications
You must be signed in to change notification settings - Fork 0
/
appendixB.qmd
92 lines (68 loc) · 2.92 KB
/
appendixB.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Appendix B: Quarto"
author: "Your Name"
date: 'Rendered on `r Sys.Date()`'
format:
html:
toc: true
toc-float: true
number-sections: true
df-print: paged # default, kable, tibble, paged, or a custom function
code-folding: show # null, hide, show
self-contained: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
out.width = '100%'
)
# packages used ----
library(tidyr) # for data processing
library(dplyr) # for data processing
library(readr) # for reading/saving data
library(ggplot2) # for plots
library(kableExtra) # for tables
# load external scripts ----
source("R/functions.R")
```
You can reference figures or tables using the chunk name, e.g., Figure @fig-disp-hp-vs and Table @tbl-mtcars. In quarto, figure chunk names must start with "fig-" and tables must start with "tbl-".
## Data Processing
Scripts typically start with the code to transform raw data into processed data. Time-consuming code may be set to not evaluate and the results saved and loaded in a separate code chunk.
```{r, eval = FALSE}
# calculate means by number of gears and carburetors
aggregated <- mtcars |>
group_by(gear, carb) |>
summarise(mpg = mean(mpg),
disp = mean(disp),
hp = mean(hp),
.groups = "drop")
write_csv(aggregated, "data/aggregated.csv")
```
```{r}
# load saved data
aggregated <- read_csv("data/aggregated.csv", show_col_types = FALSE)
```
## Tables
If you use `knitr::kable()` or `kabelExtra::kable()` to display tables as HTML, they are added to the auto-numbered tables. In Quarto you can add the caption in the code chunk.
```{r tbl-mtcars}
#| tbl-cap: Mean miles per gallon (mpg), displacement (disp) and horsepower (hp) by number of forward gears (gear) and carburetors (carb)
table1 <- aggregated |>
kable(digits = 1) |>
kable_material()
save_kable(table1, "tables/table1.html")
table1
```
## Figures
The fig-alt option sets alt-text for screen readers. This should allow a person with low or no vision to understand the meaning of the plot.
```{r fig-disp-hp-vs}
#| fig.cap: The relationship between displacement and horsepower for V-shaped and straight engines.
#| fig.alt: A scatterplot with Displacement (cu.in.) along the x-axis, ranging from ~50 to ~500, and Gross Horsepower along the y-axis, ranging from ~50 to ~350. 15 red points are plotted for V-shaped engines and 12 blue points for straight engines. There is a positive relationship between displacement and horsepower, with the V-shaped engines having typically higher values on both than straight engines.
ggplot(mtcars, aes(x = disp, y = hp, color = as.factor(vs))) +
geom_point() +
labs(x = "Displacement (cu.in.)",
y = "Gross Horsepower") +
scale_color_manual(name = "Engine",
labels = c("V-shaped", "straight"),
values = c("firebrick", "dodgerblue3"))
ggsave("figures/plot1.png", width = 7, height = 5)
```