-
Notifications
You must be signed in to change notification settings - Fork 2
/
2-starwars_characters_final.Rmd
107 lines (64 loc) · 1.98 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
title: "Star Wars Character Data"
author: "Angela Zoss"
date: "3/20/2019"
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))
```
## 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(~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)
```
## 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)
```
## Add a label to (only) the shortest character
```{r}
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) +
geom_text(data=starwars_chars %>% dplyr::filter(height < 75), aes(label=name), nudge_y = 50)
# or
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) | height == min(height, na.rm=T)), aes(label=name), nudge_y = 50)
```