-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-starwars_characters_final.Rmd
More file actions
executable file
·144 lines (86 loc) · 2.88 KB
/
2-starwars_characters_final.Rmd
File metadata and controls
executable file
·144 lines (86 loc) · 2.88 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: "Star Wars Character Data"
author: "Angela Zoss"
date: "9/7/2018"
output: github_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 %>% 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 %>% filter(mass > 1000), aes(label=name), nudge_y = -50) +
geom_text(data=starwars_chars %>% 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 %>% filter(!is.na(mass)) %>% filter(mass == max(mass) | height == min(height)), aes(label=name), nudge_y = 50)
```
## Create a new plot to show each character by their age (birth_year)
```{r}
# hint: many characters have NA for birth_year;
# try removing those characters before plotting
ggplot(starwars_chars %>% filter(!is.na(birth_year)), aes(birth_year, name)) +
geom_point()
```
## Sort the characters by their age
```{r}
# hint: the forcats package (which is included in tidyvers)
# has a useful function called fct_reorder that can be used on factors
ggplot(starwars_chars %>% filter(!is.na(birth_year)), aes(birth_year, fct_reorder(as_factor(name),birth_year))) +
geom_point()
```
## Relabel the axes if they are difficult to read
```{r}
# hint: the forcats package (which is included in tidyvers)
# has a useful function called fct_reorder that can be used on factors
ggplot(starwars_chars %>% filter(!is.na(birth_year)), aes(birth_year, fct_reorder(as_factor(name),birth_year))) +
geom_point() +
labs(y="name", x="age")
```