-
Notifications
You must be signed in to change notification settings - Fork 1
/
03_pivot.qmd
53 lines (38 loc) · 1.01 KB
/
03_pivot.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
---
title: "03 pivot data"
author: "John Little"
date-modified: 'today'
date-format: long
format:
html:
footer: "CC BY 4.0 John R Little"
license: CC BY
---
```{r}
library(tidyverse)
```
## data
These exercises use the following [`ggplot2` training datasets](https://ggplot2.tidyverse.org/reference/index.html#section-data)
- ggplot2::economics
## Pivot
Below are two data frames. One is wide data, the other is long.
```{r}
economics
economics_long %>% arrange(date)
```
## Goal
Using one of the dplyr pivot functions, pivot the economics data to long format
```{r}
economics %>%
pivot_------(cols = pce:unemploy,
names_to = "variable",
values_to = "value")
```
Now that the data are long. Can you use the `facet_wrap()` function to make multiple line plots, one line plot for each `variable` category?
```{r}
economics |>
pivot_longer(-date, names_to = "variable", values_to = "value") |>
ggplot(aes(date, value)) +
geom____() +
facet_wrap(vars(_______))
```