-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootcamp-r-series-050-documents.Rmd
127 lines (100 loc) · 5.35 KB
/
bootcamp-r-series-050-documents.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
---
title: "Creating Polished Documents"
output:
html_document: default
---
```{r setup, include=FALSE}
library(tidyverse)
library(gapminder)
library(kableExtra)
```
Today's workshop was mostly a high level overview of using the `ggplot2` and `kableExtra` packages to produce clean looking graphs and tables. These tables can then be rendered within an R Markdown document and shared in various formats.
## Professional Plots using ggplot2
The `ggplot2` package is a graphing library based on the *grammar of graphics* (gg) and part of the `tidyverse` collection of packages. The grammar of graphics is a way to build up a plot by mapping variable to a *aesthetics* such as points, lines, colors, facets, scales, etc.
Here is one example of incrementally building a plot:
```{r, }
# Creates an empty plot
iris %>%
ggplot()
# Mapping variables to the x and y axes
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width))
# Adding a geometry layer (geom_)
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
# Mapping the species variable to the point color
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point()
# Adding in extras to make a professional-looking graphic
iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point(size = 3) +
scale_color_viridis_d() +
labs(title = "Iris Flower Species",
subtitle = "Petal width v. length",
x = "Length (cm)",
y = "Width (cm)") +
theme_bw()
```
### List of Resources
There is so much that can be done with `ggplot2` and supporting libraries. Here are some resources to get started and extend to your graphing needs:
- [R for data science](https://r4ds.had.co.nz/data-visualisation.html) chapter on visualization
- [The R Graph Gallery](https://www.r-graph-gallery.com/)
- [ggfortify](https://github.com/sinhrks/ggfortify) to "to visualize statistical result of popular R packages"
- [GGally](http://ggobi.github.io/ggally/) "extends ggplot2 by adding several functions to reduce the complexity of combining geoms with transformed data"
- [ggforce](https://ggforce.data-imaginist.com/) "is a package aimed at providing missing functionality to ggplot2"
- [patchwork](https://patchwork.data-imaginist.com/) "to make it ridiculously simple to combine separate ggplots into the same graphic"
- [gganimate](https://gganimate.com/) "to include the description of animation"
## Pretty Looking Tables with `kable`
Printing a data frame to the screen can be ugly and uninspiring:
```{r}
gapminder %>%
head(10)
```
We can prepare this same data frame for publication by passing it through the `kable` function (provided by `knitr` and rexported by `kableExtra`) and the `kable_styling` function (provided by `kableExtra`):
```{r}
gapminder %>%
head(10) %>%
kable(digits = 2) %>%
kable_styling(bootstrap_options = c("striped", "hover"))
```
Here's an example that would be good for PDF output:
```{r, eval=FALSE}
# NOT RUN
gapminder %>%
filter(year > 1990) %>%
rename(Continent = continent, Year = year) %>%
group_by(Continent, Year) %>%
summarise(`Average Life Expectancy` = mean(lifeExp)) %>%
kable(format = "latex", digits = 2) %>%
kable_styling(latex_options = c("striped", "hold_position")) %>%
collapse_rows(columns = c(1, 2))
```
### List of Resources
- [R Markdown cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/kable.html) chapter on kable
- [kableExtra vignette](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html)
## All the Documents
Finally your pretty looking graphs and tables need a place to be proudly displayed. R Markdown is the solution! R Markdown is a document format the can mix R code and Markdown text formatting, as well as support for a few extra features such as directly embedding HTML, LaTeX, and Mathjax (for math typesetting).
The use cases for R Markdown are endless, and it would be impossible and an exercise in futility to try and go over all the possibilities. Instead we provide a list of the possible documents that can be created with R Markdown, and the resources for getting more help.
### Document Formats
- Simple HTML page
- PDF Document
- Slide shows via [ioslides]()
- HTML Book via [bookdown](https://bookdown.org/home/)
- Posters via [posterdown](https://github.com/brentthorne/posterdown)
- Common journal article templates via [rticles](https://github.com/rstudio/rticles)
- Blog format websites via [blogdown](https://bookdown.org/yihui/blogdown/)
- Beautiful package documentation websites via [pkgdown](https://pkgdown.r-lib.org/)
### List of Resources
- [knitr options](https://yihui.org/knitr/options/)
- An overview of all the knitr chunk options
- [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/)
- A detailed reference on the built-in R Markdown output formats of the rmarkdown package, as well as several other extension packages
- [R Markdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/)
- Provides more practical and relatively short examples to show the interesting and useful usage of R Markdown
- [Authoring Books and Technical Documents with R Markdown](https://bookdown.org/yihui/bookdown/)
- The general guide to working with bookdown
- [R Markdown Reference Guide](https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf)
- [R Markdown from RStudio](https://rmarkdown.rstudio.com/index.html)