-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIris_Example.Rmd
More file actions
164 lines (103 loc) · 4.41 KB
/
Copy pathIris_Example.Rmd
File metadata and controls
164 lines (103 loc) · 4.41 KB
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
---
title: "Iris Example"
author: "DSI Workshop"
output:
html_document:
toc: true
theme: spacelab
pdf_document: default
---
***
# First Section: Iris Violin Plot
## Subsection: Original Code
* Let's revisit the violin plot of the Iris data from the `ggplot2` section of the workshop.
* The code can be embedded in the R Markdown document. When the document is compiled, the code will be executed (`eval = TRUE` in the Rmd source) and the resulting plot will be included in the document.
* The code itself is also printed because of the option `echo = TRUE` (also in the Rmd source).
* The original code from Lingge was:
```{r echo = TRUE, eval = TRUE, warning = FALSE, fig.width = 5, fig.height = 2.5, fig.align = 'center'}
library(ggplot2)
ggplot(data = iris, aes(x = Species, y = Petal.Width)) +
geom_violin(aes(fill = Species)) +
labs(title = 'Iris Petal Width')
```
## Using Chunk Options
Some important chunk options and [default values] are:
* `eval [TRUE]`: Should the chunk be evaluated
* `echo [TRUE]`: Should the chunk be printed
* `warning [TRUE]`: Should warnings be printed
* `error [FALSE]`: Should messages be printed
* `tidy [FALSE]`: Should the code be reformatted for ease of display
* `results ["markup"]`: Possible values are "markup", "asis", "hold", "hide"
* `cache [FALSE]`: Should results be cached for future render
* `comment ["##"]`: Prefix character when displaying results
* `fig.width [7]`: Width in inches for plots from chunk
* `fig.height [7]`: Height in inches for plots from chunk
* `fig.align ["right"]`: Alignment of figures
<small>[Adapted from the R Markdown Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf)</small>
### Subsubsection: Violin Plot
Change the code chunk options so that the code is not displayed and the plot is larger.
```{r echo = FALSE, eval = TRUE, warning = FALSE, fig.width = 7, fig.height = 4.5}
library(ggplot2)
ggplot(data = iris, aes(x = Species, y = Petal.Width)) +
geom_violin(aes(fill = Species)) +
labs(title = 'Iris Petal Width')
```
***
# Images
#### Format to embed images: ``

<br><br>
#### To have more control over display options, we can use a code chunk:
```{r iris_image, out.width = "100px"}
knitr::include_graphics('Figures/iris.png')
```
<br><br>
#### Alternatively, we could use HTML to embed the image.
`<img src="Figures/iris.png" alt="Iris" style="align:right;width:604px;height:128px;">`
<img src="Figures/iris.png" alt="Iris" style="align:right;width:604px;height:128px;">
<br><br>
***
# Some Other R Markdown Features
## Displaying Math
We can easily display mathematical symbols and equations using $\LaTeX$ style formating:
* Here's an example of an inline equation: $x^2 + y^2 = z^2$.
* We can use many nice $\LaTeX$ options such as Greek letters ($\phi, \alpha, \Sigma$) and math notation ($\int, \infty, \|\cdot\|, \nabla$)
* We can also use display math formats:
$$\sum_{i = 0}^{\infty} \frac{\lambda^x e^{-\lambda}}{x!} = 1$$
***
<br><br>
## Inline Code
Similar to math, code can be included inline as well: Two plus two equals `r 2 + 2`.
* The language must be specified for the code to be executed,
+ Displayed only: `levels(iris$Species)`
+ Evaluated: `r levels(iris$Species)`
***
<br><br>
## Displaying Data
#### Display data as it would be printed from the `R` console:
```{r display_raw}
head(iris)
```
#### Display data in Table format
##### Using `knitr` and `kable`
```{r knitr_table}
knitr::kable(
head(iris),
caption = "A knitr kable."
)
```
##### Using `xtable` package
```{r results = "asis"}
print(xtable::xtable(head(iris), caption = "Table with xtable"), type = "html", html.table.attributes = "border=1")
```
##### Using `stargazer` package
```{r results = "asis"}
stargazer::stargazer(head(iris), type = "html", title = "Stargazer Table")
```
***
<br><br>
## Other Tips and Tools
* Can choose a theme for the page style or can provide your own css file
* Can use inline <strong>HTML</strong> to <i>format text</i> and include things like links: <a href="https://http://rmarkdown.rstudio.com/rmarkdown_websites.html#overview">More on Building Websites with R Markdown</a>
* The `bookdown` package can be used to typeset a book using R Markdown.
* Presentations can be created in R Markdown as well using the output document types: `ioslides_presentation`, `slidy_presentation`, `beamer_presentation`, or `xaringan`