Skip to content

Commit

Permalink
Merge pull request #50 from reconhub/slide_bag
Browse files Browse the repository at this point in the history
Slide bag
  • Loading branch information
zkamvar authored Nov 16, 2018
2 parents 1e88ce8 + 95966b4 commit afd6b4e
Show file tree
Hide file tree
Showing 15 changed files with 850 additions and 33 deletions.
69 changes: 69 additions & 0 deletions content/post/lecture-bag-of-slides.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: A bag of slides
author: Thibaut Jombart
authors: ["Thibaut Jombart"]
categories: ["lectures"]
topics: ["rmarkdown", "data science", "reproducibility", "ggplot2"]
date: 2018-11-15
image: img/highres/suitcase.jpg
slug: bag_of_slides
showonlyimage: true
css: css/custom.css
licenses: CC-BY
---


```{r options, include = FALSE}
library(knitr)
opts_chunk$set(collapse = TRUE)
```


# Collection of miscellaneous slides

This is a collection of short slide decks on various topics, too short to form a
lecture, but useful every now and then.



## Importing data into R

**Click on the image below to access the slides:**

<center>
<a href="../../slides/slides_bag/import_data.html"><img class="gateway" src="../../img/highres/old_photographs.jpg" width="50%" alt="click there for slides" align="middle"></a>
</center>




## Graphics using *ggplot2*

**Click on the image below to access the slides:**

<center>
<a href="../../slides/slides_bag/ggplot2.html"><img class="gateway" src="../../img/highres/old_photographs.jpg" width="50%" alt="click there for slides" align="middle"></a>
</center>





# About this document

## Contributors

- Thibaut Jombart: initial version

Contributions are welcome via [pull requests](https://github.com/reconhub/learn/pulls). The source files include:

- [**import data slides**](https://raw.githubusercontent.com/reconhub/learn/master/static/slides/slides_bag/import_data.Rmd)

- [**this post**](https://raw.githubusercontent.com/reconhub/learn/master/content/post/bag_of_slides.Rmd)



## Legal stuff

**License**: [CC-BY](https://creativecommons.org/licenses/by/3.0/)
**Copyright**: Thibaut Jombart, 2017
53 changes: 53 additions & 0 deletions content/post/lecture-bag-of-slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: A bag of slides
author: Thibaut Jombart
authors: ["Thibaut Jombart"]
categories: ["lectures"]
topics: ["rmarkdown", "data science", "reproducibility", "ggplot2"]
date: 2018-11-15
image: img/highres/suitcase.jpg
slug: bag_of_slides
showonlyimage: true
css: css/custom.css
licenses: CC-BY
---

Collection of miscellaneous slides
==================================

This is a collection of short slide decks on various topics, too short to form a lecture, but useful every now and then.

Importing data into R
---------------------

**Click on the image below to access the slides:**

<center>
<a href="../../slides/slides_bag/import_data.html"><img class="gateway" src="../../img/highres/old_photographs.jpg" width="50%" alt="click there for slides" align="middle"></a>
</center>
Graphics using *ggplot2*
------------------------

**Click on the image below to access the slides:**

<center>
<a href="../../slides/slides_bag/ggplot2.html"><img class="gateway" src="../../img/highres/old_photographs.jpg" width="50%" alt="click there for slides" align="middle"></a>
</center>
About this document
===================

Contributors
------------

- Thibaut Jombart: initial version

Contributions are welcome via [pull requests](https://github.com/reconhub/learn/pulls). The source files include:

- [**import data slides**](https://raw.githubusercontent.com/reconhub/learn/master/static/slides/slides_bag/import_data.Rmd)

- [**this post**](https://raw.githubusercontent.com/reconhub/learn/master/content/post/bag_of_slides.Rmd)

Legal stuff
-----------

**License**: [CC-BY](https://creativecommons.org/licenses/by/3.0/) **Copyright**: Thibaut Jombart, 2017
33 changes: 0 additions & 33 deletions content/post/test-buttons.Rmd

This file was deleted.

Binary file added static/img/highres/old_photographs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/highres/suitcase.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/slides/iris.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/slides/magic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/slides/palette.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/slides/shot_imported_data.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img/slides/shot_spreadsheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
168 changes: 168 additions & 0 deletions static/slides/slides_bag/ggplot2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
title: "Making graphics using *ggplot2*"
author: "Thibaut Jombart"
date: "2018-11-15"
output:
ioslides_presentation:
incremental: false
---

<style>
img {
border-radius: 8px;
box-shadow: 5px 5px 5px #999;
}
</style>


```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, out.width = "60%",
warning = FALSE, message = FALSE,
tidy = FALSE, fig.align = "center")
```


## A simple example: Fisher's Iris

<center>
<img src="../../img/slides/iris.jpg" width="35%"></a>
</center>

<br>

```{r fisher}
head(iris)
```


## The basic graphics system

Functions create graphics, example with Fisher's Iris:

```{r basic-plot}
hist(iris$Petal.Length, col = "grey", border = "white",
main = "Example of a histogram", xlab = "Fisher's Iris: petal length")
```



## *ggplot2*: treating graphics as objects

Functions create a graphical object:

```{r ggplot2-plot, echo = -1}
library(ggplot2)
my_plot <- ggplot(iris) + geom_histogram(aes(Petal.Length)) +
labs(title = "Example of a histogram",
x = "Fisher's Iris: petal length")
my_plot
```



## A grammar for graphics

*ggplot2* builds graphics one component at a time. The grammar includes:

- `ggplot()`: to provide the data
- `geom_...`: to define the type of graph
- `aes()`: to map features of data into *aesthetic* properties (e.g. shapes, colors)


```{r eval = FALSE}
my_plot <- ggplot(iris) + # use 'iris' data.frame
geom_histogram(aes(Petal.Length)) + # hist using petal length
labs(title = "Example of a histogram", # add title
x = "Fisher's Iris: petal length") # add x label
```



## Making customisation easy

For instance, to color histograms by species:

```{r iris_colors}
my_plot + aes(fill = Species)
```




## Making customisation easy: scatterplot


```{r iris_scatterplot}
my_plot <- ggplot(iris, aes(x = Petal.Length, y = Sepal.Length)) +
geom_point(aes(color = Species, shape = Species),
size = 5, alpha = .75) +
geom_smooth() +
labs(title = "Example of a scatterplot")
my_plot
```



## Going further

<center>
<img src="../../img/slides/palette.jpg" width="35%"></a>
</center>

<br>

Learn more about *ggplot2*:

- check the package's [website](https://ggplot2.tidyverse.org/)
- a list of [all available `geoms_`](https://ggplot2.tidyverse.org/reference/index.html#section-layer-geoms) (types of plot)
- a list of [`aes()` specifications](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html) (colors / shapes / etc.)
- a [gallery of extensions](http://www.ggplot2-exts.org/gallery/) to *ggplot2*




## Useful tips: specifying your colors

- select your favourite colors, e.g. using a [color picker](https://www.w3schools.com/colors/colors_picker.asp)
- make a named vector storing these colors as `character`
- specify your colors using e.g.:

```{r custom_colors}
col <- c(setosa = "#666699", versicolor = "#99cc00", virginica = "#b30059")
my_plot + scale_color_manual(values = col)
```

Note that you will need to use `scale_fill_manual` for colors specified through `fill = ...`.




## Useful tips: using panels

You can use `facet_grid(rows ~ columns)` to specify panels:
```{r ggplot2_panels}
new_plot <- ggplot(iris) +
geom_histogram(aes(x = Petal.Length, fill = Species)) +
scale_fill_manual(values = col) + facet_grid(Species ~ .)
new_plot
```



## Useful tips: moving the legend

Use `theme(legend.position = c(x_coord, y_coord))` to move the legend:

```{r ggplot2_legend_position}
new_plot + theme(legend.position = c(.9, .8))
```



## Useful tips: changing the theme

Other *themes* are available and can be customised:

```{r ggplot2_themes}
new_plot + theme_light(base_family = "Times", base_size = 20)
```
272 changes: 272 additions & 0 deletions static/slides/slides_bag/ggplot2.html

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions static/slides/slides_bag/import_data.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
title: "Importing data into R"
author: "Thibaut Jombart"
date: "2018-11-15"
output:
ioslides_presentation:
incremental: false
---

<style>
img {
border-radius: 8px;
box-shadow: 5px 5px 5px #999;
}
</style>


## What data usually look like

<center>
<img src="../../img/slides/shot_spreadsheet.png" width="75%">
</center>

<br>
Typical spreadsheet:

- <font color="#99004d">rows</font>: observations (typically individuals)
- <font color="#99004d">columns</font>: variables


## What data look like in R

<center>
<img src="../../img/slides/shot_imported_data.png" width="70%">
</center>

<br>
A <font color="#99004d">`data.frame`</font> is the R equivalent of a
spreadsheet.


## How does the magic happen

<center>
<img src="../../img/slides/magic.png" width="35%">
</center>

> <font color="#99004d">Basic R:</font>
- `read.table("some_data.txt")` for general text files
- `read.csv("some_data.csv")` for csv files

> <font color="#99004d">In packages:</font>
- `readr::read_csv("some_data.csv")` for csv files
- `readxl::read_xlsx("some_data.xlsx")` for Excel format
- `haven::read_dta("some_data.dta")` for Stata
- ...

Loading

0 comments on commit afd6b4e

Please sign in to comment.