-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-section.Rmd
167 lines (128 loc) · 9.67 KB
/
03-section.Rmd
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
```{r include_packages_2, include = FALSE}
# This chunk ensures that the thesisdown package is
# installed and loaded. This thesisdown package includes
# the template files for the thesis and also two functions
# used for labeling and referencing
if (!require(remotes)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("remotes", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("remotes")',
"first in the Console."
)
)
}
}
if (!require(dplyr)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("dplyr", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("dplyr")',
"first in the Console."
)
)
}
}
if (!require(ggplot2)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("ggplot2", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("ggplot2")',
"first in the Console."
)
)
}
}
if (!require(bookdown)) {
if (params$`Install needed packages for {thesisdown}`) {
install.packages("bookdown", repos = "https://cran.rstudio.com")
} else {
stop(
paste(
'You need to run install.packages("bookdown")',
"first in the Console."
)
)
}
}
if (!require(thesisdown)) {
if (params$`Install needed packages for {thesisdown}`) {
remotes::install_github("ismayc/thesisdown")
} else {
stop(
paste(
"You need to run",
'remotes::install_github("ismayc/thesisdown")',
"first in the Console."
)
)
}
}
library(thesisdown)
library(dplyr)
library(ggplot2)
library(knitr)
flights <- read.csv("data/flights.csv", stringsAsFactors = FALSE)
```
# Graphics, References, and Labels {#ref-labels}
## Figures
If your thesis has a lot of figures, _R Markdown_ might behave better for you than that other word processor. One perk is that it will automatically number the figures accordingly in each section. You'll also be able to create a label for each figure, add a caption, and then reference the figure in a way similar to what we saw with tables earlier. If you label your figures, you can move the figures around and _R Markdown_ will automatically adjust the numbering for you. No need for you to remember! So that you don't have to get too far into LaTeX to do this, a couple **R** functions have been created for you to assist. You'll see their use below.
<!--
One thing that may be annoying is the way _R Markdown_ handles "floats" like tables and figures (it's really \LaTeX's fault). \LaTeX\ will try to find the best place to put your object based on the text around it and until you're really, truly done writing you should just leave it where it lies. There are some optional arguments specified in the options parameter of the `label` function. If you need to shift your figure around, it might be good to look here on tweaking the options argument: <https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions>
If you need a graphic or tabular material to be part of the text, you can just put it inline. If you need it to appear in the list of figures or tables, it should be placed in a code chunk.
-->
In the **R** chunk below, we will load in a picture stored as `YSU.png` in our main directory. We then give it the caption of "YSU logo", the label of "ysulogo", and specify that this is a figure. Make note of the different **R** chunk options that are given in the R Markdown file (not shown in the knitted document).
```{r ysulogo, fig.cap="YSU logo", out.width="0.2\\linewidth", fig.align="center"}
include_graphics(path = "figure/YSU.png")
```
<!-- Note the use of `out.width` as a chunk option here. The resulting
image is 20% of what the linewidth is in LaTeX. You can also center
the image using `fig.align="center"` as shown.-->
Here is a reference to the YSU logo: Figure \@ref(fig:ysulogo). Note the use of the `fig:` code here. By naming the **R** chunk that contains the figure, we can then reference that figure later as done in the first sentence here. We can also specify the caption for the figure via the R chunk option `fig.cap`.
\clearpage
<!-- clearpage ends the page, and also dumps out all floats.
Floats are things like tables and figures. -->
Below we will investigate how to save the output of an **R** plot and label it in a way similar to that done above. Recall the `flights` dataset from Chapter \@ref(rmd-basics). (Note that we've shown a different way to reference a section here.) We will next explore a bar graph with the mean flight departure delays by airline from Portland for 2014.
```{r delaysboxplot, warnings=FALSE, messages=FALSE, fig.cap="Mean Delays by Airline", fig.width=6, fig.height=5}
mean_delay_by_carrier <- flights %>%
group_by(carrier) %>%
summarize(mean_dep_delay = mean(dep_delay))
ggplot(mean_delay_by_carrier, aes(x = carrier, y = mean_dep_delay)) +
geom_bar(position = "identity", stat = "identity", fill = "red")
```
Here is a reference to this image: Figure \@ref(fig:delaysboxplot).
A table linking these carrier codes to airline names is available at <https://github.com/ismayc/pnwflights14/blob/master/data/airlines.csv>.
\clearpage
Next, we will explore the use of the `out.extra` chunk option, which can be used to shrink or expand an image loaded from a file by specifying `"scale= "`. Here we use the mathematical graph stored in the "subdivision.pdf" file.
```{r subd, results="asis", echo=FALSE, fig.cap="Subdiv. graph", out.extra="scale=0.75"}
include_graphics("figure/subdivision.pdf")
```
Here is a reference to this image: Figure \@ref(fig:subd). Note that `echo=FALSE` is specified so that the **R** code is hidden in the document.
**More Figure Stuff**
Lastly, we will explore how to rotate and enlarge figures using the `out.extra` chunk option. (Currently this only works in the PDF version of the book.)
```{r subd2, results="asis", echo=FALSE, out.extra="angle=180, scale=1.1", fig.cap="A Larger Figure, Flipped Upside Down"}
include_graphics("figure/subdivision.pdf")
```
As another example, here is a reference: Figure \@ref(fig:subd2).
## Footnotes and Endnotes
You might want to footnote something. ^[footnote text] The footnote will be in a smaller font and placed appropriately. Endnotes work in much the same way.
## Bibliographies
Of course you will need to cite things, and you will probably accumulate an armful of sources. There are a variety of tools available for creating a bibliography database (stored with the .bib extension). In addition to BibTeX suggested below, you may want to consider using the free and easy-to-use tool called Zotero. The librarians at Reed College have created Zotero documentation at <https://libguides.reed.edu/citation/zotero>. In addition, a tutorial is available from Middlebury College at <https://sites.middlebury.edu/zoteromiddlebury/>.
_R Markdown_ uses _pandoc_ (<https://pandoc.org/>) to build its bibliographies. One nice caveat of this is that you won't have to do a second compile to load in references as standard LaTeX requires. To cite references in your thesis (after creating your bibliography database), place the reference name inside square brackets and precede it by the "at" symbol. For example, here's a reference to a book about worrying: [@Molina1994]. This `Molina1994` entry appears in a file called `thesis.bib` in the `bib` folder. This bibliography database file was created by a program called BibTeX. You can call this file something else if you like (look at the YAML header in the main .Rmd file) and, by default, is to placed in the `bib` folder.
For more information about BibTeX and bibliographies, see the CUS site at Reed College (<https://web.reed.edu/cis/help/latex/index.html>)^[@reedweb2007]. There are three pages on this topic: _bibtex_ (which talks about using BibTeX, at <https://web.reed.edu/cis/help/latex/bibtex.html>), _bibtexstyles_ (about how to find and use the bibliography style that best suits your needs, at <https://web.reed.edu/cis/help/latex/bibtexstyles.html>) and _bibman_ (which covers how to make and maintain a bibliography by hand, without BibTeX, at <https://web.reed.edu/cis/help/latex/bibman.html>). The last page will not be useful unless you have only a few sources.
If you look at the YAML header at the top of the main .Rmd file you can see that we can specify the style of the bibliography by referencing the appropriate csl file. You can download a variety of different style files at <https://www.zotero.org/styles>. Make sure to download the file into the csl folder.
<!-- Fill the rest of the page with the content below for the PDF version. -->
\vfill
**Tips for Bibliographies**
- Like with thesis formatting, the sooner you start compiling your bibliography for something as large as thesis, the better. Typing in source after source is mind-numbing enough; do you really want to do it for hours on end in late April? Think of it as procrastination.
- The cite key (a citation's label) needs to be unique from the other entries.
- When you have more than one author or editor, you need to separate each author's name by the word "and" e.g. `Author = {Noble, Sam and Youngberg, Jessica},`.
- Bibliographies made using BibTeX (whether manually or using a manager) accept LaTeX markup, so you can italicize and add symbols as necessary.
- To force capitalization in an article title or where all lowercase is generally used, bracket the capital letter in curly braces.
- You can add a YSU Thesis citation^[@noble2002] option. The best way to do this is to use the phdthesis type of citation, and use the optional "type" field to enter "YSU thesis" or "Undergraduate thesis."