-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-visualization-solutions.R
275 lines (214 loc) · 8.52 KB
/
data-visualization-solutions.R
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Scatterplots ------------------------------------------------------------
# Make a scatterplot that shows flipper length on the x axis and body mass on the y axis.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point()
# Histograms --------------------------------------------------------------
# Make a histogram that shows the distribution of the body_mass_g variable.
ggplot(data = penguins,
mapping = aes(x = body_mass_g)) +
geom_histogram()
# Adjust your histogram so it has 50 bins.
ggplot(data = penguins,
mapping = aes(x = body_mass_g)) +
geom_histogram(bins = 50)
# Bar Charts --------------------------------------------------------------
# Use the v1 approach to make a bar chart that shows a count of the number of penguins by species.
ggplot(data = penguins,
mapping = aes(x = species)) +
geom_bar()
# Use the v2 approach by doing the following:
# 1. Creating a new data frame called penguins_by_year that is a
# count of the number of penguins by species
# 2. Plot your data frame using the v2 approach with geom_bar()
penguins_by_species <- penguins |>
count(species)
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n)) +
geom_bar(stat = "identity")
# Make the same graph as above, but use geom_col() instead of geom_bar()
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n)) +
geom_col()
# Setting color and fill Aesthetic Properties -----------------------------
# Take your graph that uses geom_col() and make the inside of each bar a different color.
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species)) +
geom_col()
# Make your scatterplot from before with flipper length on the x axis and body mass on the y axis
# but make the points different colors based on the island variable
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm,
y = body_mass_g,
color = island)) +
geom_point()
# Setting color and fill Scales -------------------------------------------
# Take your scatterplot that you just made and add a scale using scale_color_manual().
# You can find a list of all colors you can use here:
# http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm,
y = body_mass_g,
color = island)) +
geom_point() +
scale_color_manual(values = c("red", "blue", "green"))
# Now update the last bar chart you made by manually specifying colors of the bars
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species)) +
geom_col() +
scale_fill_manual(values = c("red", "blue", "green"))
# Update your bar chart using the scale_fill_viridis_d() function instead of
# scale_fill_manual()
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species)) +
geom_col() +
scale_fill_viridis_d()
# Setting x and y Scales --------------------------------------------------
# Copy the code for the last bar chart you made
# Update it so that the y axis goes from 0 to 200
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species)) +
geom_col() +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160))
# Copy the code you just wrote
# Update it so that it has breaks on the y axis at 0, 40, 80, 120, and 160
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species)) +
geom_col() +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160))
# Adding Text to Plots ---------------------------------------------------------
# Copy your last code chunk.
# Then add text labels on the top of each bar that show the number of penguins of each species.
# You'll need to use geom_text() and the vjust argument to do this.
# Make the text labels show up in red.
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species,
label = n)) +
geom_col() +
geom_text(vjust = -0.5,
color = "red") +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160))
# Do the same thing, but use geom_label() instead of geom_text().
# This time, make the text itself show up in white.
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species,
label = n)) +
geom_col() +
geom_label(vjust = -0.5,
color = "white") +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160))
# Plot Labels -------------------------------------------------------------
# Copy the code for the last plot you made that uses geom_label().
# Then do the following:
# 1. Add a title
# 2. Remove the x and y axis labels
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species,
label = n)) +
geom_col() +
geom_label(vjust = -0.5,
color = "white") +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160)) +
labs(title = "Number of penguins by species",
x = NULL,
y = NULL)
# Themes ------------------------------------------------------------------
# Use one of the built-in ggplot2 themes to change the look and feel of your last plot
# https://ggplot2.tidyverse.org/reference/index.html#themes
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species,
label = n)) +
geom_col() +
geom_label(vjust = -0.5,
color = "white") +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160)) +
labs(title = "Number of penguins by species",
x = NULL,
y = NULL) +
theme_minimal()
# Install the ggthemes package
# Load the ggthemes package
# Use one of themes from the package to update your last plot
# The themes can be found here:
# https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/
library(ggthemes)
ggplot(data = penguins_by_species,
mapping = aes(x = species,
y = n,
fill = species,
label = n)) +
geom_col() +
geom_label(vjust = -0.5,
color = "white") +
scale_fill_viridis_d() +
scale_y_continuous(limits = c(0, 160),
breaks = c(0, 40, 80, 120, 160)) +
labs(title = "Number of penguins by species",
x = NULL,
y = NULL) +
theme_fivethirtyeight()
# Facets ------------------------------------------------------------------
# I've written code to give you a data frame to work with
# Run the code and take a look at the penguin_weight_by_species_and_sex data frame
penguin_weight_by_species_and_sex <- penguins |>
drop_na(sex) |>
group_by(species, sex) |>
summarize(mean_weight = mean(body_mass_g))
# Now see if you can recreate this plot:
# https://raw.githubusercontent.com/rfortherestofus/fundamentals-v2/main/plots/penguins-by-weight-species.png
# You'll need to adjust the theme, add plot labels, and use facetting.
ggplot(data = penguin_weight_by_species_and_sex,
mapping = aes(x = species,
y = mean_weight,
fill = sex)) +
geom_col() +
facet_grid(cols = vars(sex)) +
theme_minimal() +
labs(title = "Average penguin weight by sex and species",
x = NULL,
y = NULL)
# Save Plots --------------------------------------------------------------
# Copy the code from your last plot
# Save it as 10cm wide by 20cm tall png file with a white background
ggsave(filename = "plots/penguins-by-weight-species.png",
height = 10,
width = 20,
units = "cm",
bg = "white",
dpi = 300)