-
Notifications
You must be signed in to change notification settings - Fork 1
/
2-starwars_characters_final.Rmd
93 lines (55 loc) · 1.62 KB
/
2-starwars_characters_final.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
---
title: "Star Wars Character Data"
author: "Angela Zoss"
date: "1/30/2020"
output: html_document
---
## Setup your environment
```{r}
# Load required libraries
library(tidyverse)
```
## Load your data
```{r}
# built-in data
starwars_chars <- starwars
```
## Create a plot to explore the height of Star Wars characters
```{r}
# hint: height is numerical;
# look for geoms that do a good job of summarizing numerical variables
ggplot(starwars_chars) +
geom_histogram(aes(height))
```
## Facets: Add a facet to the chart to create small multiples for each gender
```{r}
# hint: try facet_wrap
ggplot(starwars_chars) +
geom_histogram(aes(height)) +
facet_wrap(vars(gender))
```
## Create a new plot to compare character heights to weights (masses)
```{r}
# what geom is best for two numerical variables?
ggplot(starwars_chars, aes(height,mass)) +
geom_point()
```
## Add a linear trend line
```{r}
# hint: look at the options for geom_smooth
ggplot(starwars_chars, aes(height,mass)) +
geom_point() +
geom_smooth(method = "lm", se=FALSE)
```
## Data inheritance: Add a label to (only) the heaviest character
```{r}
# hint: you can use "data=" in a geom layer to use different data for that layer
ggplot(starwars_chars, aes(height,mass)) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) +
geom_text(data=starwars_chars %>% dplyr::filter(mass > 1000), aes(label=name), nudge_y = -50)
ggplot(starwars_chars, aes(height,mass)) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) +
geom_text(data=starwars_chars %>% dplyr::filter(mass == max(mass, na.rm=T)), aes(label=name), nudge_y = -50)
```