generated from uviclibraries/dsc-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggplot2-data.Rmd
607 lines (457 loc) · 19 KB
/
ggplot2-data.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
---
title: "4-Data Visualization with ggplot2"
#author: DSC / Chloë Farr
#date: "2024-01-18"
output:
github_document:
toc: true
toc_depth: 3
pdf_document: default
html_document:
toc: true
toc_depth: '3'
df_print: paged
customjs: http://code.jquery.com/jquery-1.4.2.min.js
layout: default
nav_order: 5
parent: Workshop Activities
keep_md: true # Retain YAML front matter in output Markdown
---
<img src="images/rstudio-22.png" alt="rstudio logo" style="float:right;width:220px;"/>
<br>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#uncomment the install.packages if you haven't installed and loaded the 'readxl' package
#install.packages("readxl")
library(readxl)
library(tidyverse)
library(ggthemes)
library(janitor)
library(stringr)
startCodeDetailsBlock <- function(summaryText = "Check Your Code") {
# Use cat to directly output the string without quotes and avoid unintended newlines
cat(
"{::options parse_block_html='true' /}<details><summary markdown='span'>",summaryText,"</summary>"
)
}
endCodeDetailsBlock <- function() {
cat("</details>{::options parse_block_html='false'/}")
}
```
```{r, echo=FALSE, eval=FALSE}
isNamespaceLoaded("dplyr")
isNamespaceLoaded("magrittr")
isNamespaceLoaded("tidyverse")
```
```{css, echo=FALSE}
div.html-widget {
overflow-x: auto;
}
table {
display: block;
max-width: none;
white-space: nowrap;
}
```
```markdown
#move me to the top after pushing to github, and remove this comment
---
layout: default
title: 4-Data Visualization with ggplot2
nav_order: 5
parent: Workshop Activities
customjs: http://code.jquery.com/jquery-1.4.2.min.js
---
```
If you and your group have any questions or get stuck as you work through this in-class exercise, please ask the instructor for assistance. Have fun!
The `ggplot2` package is a popular system for creating data visualizations like plots, charts, graphs, etc.
In this activity, you will make a scatter plot, bar chart, and a line chart.
## 1. Getting Ready
<br>
#### [Task 1.1:]{.underline} Install and load the 'ggthemes' and 'janitor' packages.
- Package names:
- tidyverse
- ggthemes
- janitor
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
install.packages("ggthemes") #then, as always, type 'enter' or 'return' to submit the command for execution
install.packages("janitor")
library(ggthemes) #Do not wrap library() parameter string in quotes
library(janitor)
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
*Hint:* wrap the package name in `""` quotations<br>
- Do not wrap the library() parameter in `""` quotations
> More about ggthemes [here](https://exts.ggplot2.tidyverse.org/ggthemes.html){:target="\_blank"}.
> More about janitor [here](https://www.rdocumentation.org/packages/janitor/versions/2.2.0){:target="\_blank"}.
<br>
#### [Task 1.2:]{.underline} Read and clean your data set.
- Data set file name: `flavors_of_cacao.csv` (unless you changed the filename after downloading)
- Name your variable: `chocolateData`
- Clean the column header names using `clean_names()` where the parameter is chocolateData (leave parentheses blank if piping)
- Remove first (empty) row using `filter(ref != "REF")`
<br>
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r echo=TRUE, eval=FALSE}
#if your file cannot be found, enter `getwd()` into your console and it will tell you the file path you should most likely use. If you cannot find the file, use Option a.
chocolateData <- read_csv("Desktop/flavors_of_cacao.csv") %>%
clean_names() %>% #Clean the column header names
filter(ref != "REF")
#If you get a column specification error, add `, show_col_types = FALSE` as to a parameter read_csv()
#e.g. chocolateData <- read_csv("Desktop/flavors_of_cacao.csv", show_col_types = FALSE)
```
```{r load-data, echo=FALSE, results='hide'}
chocolateData <- read_csv("docs/flavors_of_cacao.csv")%>%
clean_names() %>% #Clean the column header names
filter(ref != "REF")
#If you get a column specification error, add `, show_col_types = FALSE` as to a parameter read_csv()
#e.g. chocolateData <- read_csv("~/Documents/GitHub/dsc-r-workshop/flavors_of_cacao.csv", show_col_types = FALSE)
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
*Hint:* See Activity 3, Task 3.1 for instructions on importing a csv file.
<br>
#### [Task 1.3:]{.underline} Preview the first 5 rows of your chocolate data.
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r}
#preview first 5 lines of chocolateData
chocolateData %>% head(5)
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
## 2. Creating Plots and Charts in ggplot2
Here is some information about creating and formatting plots, common to all types we will look at in this activity. Don't do anything yet!
The command to begin plots and charts are very similar. Let's first look at the commonalities.
For all of them, we will use the `ggplot()` function and a geometry function.
`ggplot()` parameters are:
- The dataset used for the plot `data = datasetName`
- The aesthetic mappings. This specifies which column values is assigned to the x axis, and which is assigned to the y axis.
- `aes(x = columnForXAxis, y = columnForYAxis)`
The geometry function is attached to the ggplot() function with ` + geom_` and is completed by the type of plot or chart.
- scatter plot or point plots: ` + geom_point()`
- bar charts: `geom_bar()`
- line charts: `geom_line()`
Plots will appear in the "Plot" tab (probably in the bottom right hand quadrant of your workspace).
### 2.1. Scatter Plots
First things first, we need to quickly clean up our dataframe for scatter plots. Copy and paste the following code into your console, and execute to imort and prepare our data.
<br>
```{r}
#remove the percentage signs from the column cocoa_percent by converting the values to numbers
chocolateData$cocoa_percent <- parse_number(chocolateData$cocoa_percent)
#make sure the data type of each column is correct.
chocolateData <- type_convert(chocolateData)
#You can ignore the Column Specification comment in the output. It indicates the column specification, which describes the data types of various columns after conversion, and shows that several columns have been confirmed as character columns.
```
<br>
Let's apply the ggplot command above to create a scatter plot.
<br>
**Definition - Scatter plot:** A plot with two axes, each representing a different variable. Each individual observation is showing using a single point. The position of the point is determined by the value of the variables assigned to the x and y axes for that observation.
<img src="images/chocolate_bar_scatter.png" alt="Chocolate bar pseudo scatter plot" style="width:420px;"/>
#### [Task 2.1.1:]{.underline} Make a scatter plot of the cocoa percentage and the rating a chocolate bar received.
- Using chocolate data : `chocolateData`
- X-axis = Cocoa percentage: `cocoa_percent`
- Y-axis = Rating a chocolate bar received: `rating`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
ggplot(data = chocolateData, aes(x = cocoa_percent, y = rating)) +
geom_point() # then add a layer of points
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
Output
```{r echo=FALSE}
ggplot(data = chocolateData, aes(x = cocoa_percent, y = rating)) +
geom_point() # then add a layer of points
```
<br>
Before we add details to our plot, we need to learn about the different components. Again, wait until the next task to do anything.
**Definition - Fitted line:** (aka. a 'line of best fit') is a line representing some function of x and y that has the best fit (or the smallest overall error) for the observed data.
<br>
Function for adding a smooth line to a plot: `geom_smooth(method = "")`
- method type specifies the type of smoothing to be used
<details><summary>Expand for more geom_smooth method types</summary>
*Linear Model ("lm"):* fits a linear regression model, suitable for linear relationships.
*Locally Estimated Scatterplot Smoothing ("loess" or "lowess")*: creates a smooth line through the plot by fitting simple models in a localized manner, which can handle non-linear relationships well. Ideal for smaller datasets
*Generalized Additive Models ("gam"):* model complex, nonlinear trends in data.Ideal for larger datasets.
*Moving Average ("ma"):* smooths data by creating an average of different subsets of the full dataset. It's useful for highlighting trends in noisy data.
*Splines ("splines"):* provide a way to smoothly interpolate between fixed points, creating a piecewise polynomial function. They are useful for fitting complex, flexible models to data.
*Robust Linear Model ("rlm"):* Similar to linear models but less sensitive to outliers. It's useful when your data contains outliers that might skew the results of a standard linear model.
</details>
<br>
- Fitted line: `method = "lm"`
<br>
#### [Task 2.1.2:]{.underline} Make another scatter plot of the cocoa percentage and the rating a chocolate bar received, with the following:
- A "line of best fit"
- Informative x and y axis labels
- A title
- Using chocolate data : `chocolateData`
- X-axis = Cocoa percentage: `cocoa_percent`
- Y-axis = Rating a chocolate bar received: `rating`
- Line of best fit: `geom_smooth(method = "lm")`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r}
ggplot(data = chocolateData, aes(x = cocoa_percent, y = rating)) +
geom_point() + # then add a layer of points
geom_smooth(method = "lm")
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
#### [Task 2.1.3:]{.underline} Add descriptive axis labels and a title to your scatter plot.
We're also going to add labels and custom colors using the `labs()` function and custom colors.
- Labels `+ labs(title = "", x = "", y = " ")`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
#you can use the following labels or make your own.
ggplot(data = chocolateData, aes(x = cocoa_percent, y = rating)) +
geom_point() + # then add a layer of points
geom_smooth(method = "lm") +
labs(title = "Rating of Chocolate Bar by Cocoa Percentage", x = "Chocolate Bar Rating", y = "Cocoa Percentage")
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
Output:
```{r echo=FALSE}
#you can use the following labels or make your own.
ggplot(data = chocolateData, aes(x = cocoa_percent, y = rating)) +
geom_point() + # then add a layer of points
geom_smooth(method = "lm") +
labs(title = "Rating of Chocolate Bar by Cocoa Percentage", x = "Chocolate Bar Rating", y = "Cocoa Percentage")
```
### 2.2. Bar Charts
First things first, we need to quickly clean up our dataframe for bar charts. Copy and paste the following code into your console, and execute.
<br>
```{r}
chocolateData$bean_type_simplified <- word(chocolateData$bean_type, 1)
chocolateData$bean_type_simplified <- gsub('[[:punct:]]', '', chocolateData$bean_type_simplified)
chocolateData$bean_type_simplified <- trimws(chocolateData$bean_type_simplified)
chocolateData <- chocolateData %>%
filter(str_detect(bean_type_simplified, "\\S")) # This ensures the string contains at least one non-whitespace character
commonBeanTypes <- chocolateData %>%
select(bean_type_simplified) %>%
group_by(bean_type_simplified) %>%
count() %>%
filter(n > 20) %>%
ungroup() %>%
mutate(bean_type_simplified = as.factor(bean_type_simplified))
# Filter chocolateData to only include common beans
chocolateData_commonBeans <- chocolateData %>%
filter(bean_type_simplified %in% commonBeanTypes$bean_type_simplified)
```
<br>
A bar chart illustrates *categories* along the x axis and the count of observations from each category on the y axis.
To make a bar chart, you need the data (categories, and values relevate to those categories), and the categories the data will be separated by (each representing one bar).
The first 5 rows of the bars made of common beans:
```{r, echo=FALSE}
chocolateData_commonBeans %>% head(5)
```
The bars will represent the following categories:
```{r, echo=FALSE}
commonBeanTypes
```
With the code above, you now have:
- A dataset `chocolateData_commonBeans`: containing the chocolate bars made with the most common beans
- A vector `commonBeanTypes` list of the common bean types, which will be used as the categories for the x-axis.
#### [Task 2.2.1:]{.underline} Create a basic bar chart
Your chart will illustrate the frequency that chocolate bars are being made in different countries.
- Country bar was made in: `broad_bean_origin`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
ggplot(chocolateData_commonBeans, aes(x = chocolateData_commonBeans$bean_type_simplified)) + geom_bar()
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
*Hint:* geom type = "bar"
<br>
Output:
```{r echo=FALSE}
ggplot(chocolateData_commonBeans, aes(x = chocolateData_commonBeans$bean_type_simplified)) + geom_bar()
```
<br>
#### [Task 2.2.2:]{.underline} Create a stacked bar chart
A stacked bar chart shows two dimensions (variables) of data. Each bar will represent one variable, and each bar will be chopped into sections which represent a second variable.
To add a second dimension,
- following the same command as the bar chart above, modify it by:
- adding the parameter `fill=~factor2name` to `aes()`, where 'factor2name' is the second variable's column name.
- setting the parameter of `geom_bar()` to `position="stack"`
<br>
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
ggplot(chocolateData_commonBeans, aes(x = bean_type_simplified, fill = company_location)) +
geom_bar(position = "stack")
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
Output:
```{r echo=FALSE}
ggplot(chocolateData_commonBeans, aes(x = bean_type_simplified, fill = company_location)) +
geom_bar(position = "stack")
```
<br>
<!--**Definition - facets:** A way of breaking apart a plot of a specific data frame so that each level of a the target factor is shown in a separate, smaller chart.
<br>
A faceted bar chart is like a grid of mini bar charts, each showing a different slice of the data side by side for comparison.
We can facet a bar chart in a few different ways.
Where the previous bar chart has one piece of information in each bar, we will now add two.
- Instead of `aes(x=)` representing just the categories that each bar will represent, we will add a `fill=` parameter for the subgroups of each column.
- `ggplot(data, aes(x = category, fill = subgroup))`
We will then add
`+ geom_bar(position = "fill") + facet_wrap(~facet_variable)`
-`geom_bar()` creates a stacked bar chart with proportions
- 'fill' means that each proportion of the bar will total to 100%
<details><summary>Check Your Code</summary>
```{r eval=FALSE}
ggplot(chocolateData_commonBeans, aes(x = chocolateData_commonBeans$bean_type_simplified)) + geom_bar(position = "fill") + facet_wrap(~facet_variable)
```
</details>
*Hint:* geom type = "bar"
<br> -->
### 2.3. Line Charts
#### [Task 2.2.3:]{.underline} Create a variable with the mean chocolate rating by year.
Using piping, create a new variable, `meanRatingByYear`
- base data: `chocolateData`
- group_by: `review_date`
- use `summarise()`
- the parameter is `rating=mean(rating)`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
meanRatingByYear <- chocolateData %>% group_by(review_date)%>%summarise(rating=mean(rating))
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
Your output will be:
```{r echo=FALSE}
meanRatingByYear <- chocolateData %>% group_by(review_date)%>%summarise(rating=mean(rating))
```
Then convert "review_date to Date class by entering
```{r}
meanRatingByYear$review_date <- as.integer(meanRatingByYear$review_date)
```
#### [Task 2.2.3:]{.underline} Create a line chart using the mean chocolate rating by year.
Here we'll make a line chart to show how the mean rating of chocolate has changed by year.
- Your base data will be the mean rating table you just created
- the x axis value will be the review date
- the y axis will be the rating
- the geom type is `line`, with no parameter
After the geom type, add:
`ggplot(meanRatingByYear, aes(x = review_date, y = rating)) + geom_line()+ scale_x_continuous(breaks = meanRatingByYear$review_date, labels = as.character(meanRatingByYear$review_date))`
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
ggplot(meanRatingByYear, aes(x = review_date, y = rating)) +
geom_line()+ scale_x_continuous(
breaks = meanRatingByYear$review_date, # Use actual review dates for breaks
labels = as.character(meanRatingByYear$review_date) # Convert to character to avoid decimals
)
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
Output:
```{r echo=FALSE}
ggplot(meanRatingByYear, aes(x = review_date, y = rating)) +
geom_line()+ scale_x_continuous(
breaks = meanRatingByYear$review_date, # Use actual review dates for breaks
labels = as.character(meanRatingByYear$review_date) # Convert to character to avoid decimals
)
```
<br>
#### [Task 2.2.4:]{.underline} Style your line chart.
Using the same chart you just made, add some stylistic features and modifications.
- rename the x label to "Review Date"
- rename the y label to "Rating"
- Add a title using `ggtitle()` : "Change in Rating Over Time
```{r echo = F, results='asis'}
startCodeDetailsBlock(summaryText = "Check Your Code")
```
```{r eval=FALSE}
ggplot(meanRatingByYear, aes(x = review_date, y = rating)) +
geom_line() +
scale_x_continuous(
breaks = meanRatingByYear$review_date, # Use actual review dates for breaks
labels = as.character(meanRatingByYear$review_date) # Convert to character to avoid decimals
) +
labs(
x = "Review Date",
y = "Rating",
title = "Change in Rating Over Time"
)
```
```{r echo = F, results='asis'}
endCodeDetailsBlock()
```
<br>
Output:
```{r echo=FALSE}
ggplot(meanRatingByYear, aes(x = review_date, y = rating)) +
geom_line() +
scale_x_continuous(
breaks = meanRatingByYear$review_date, # Use actual review dates for breaks
labels = as.character(meanRatingByYear$review_date) # Convert to character to avoid decimals
) +
labs(
x = "Review Date",
y = "Rating",
title = "Change in Rating Over Time"
)
```
```{=html}
<script>
function toggle(input) {
var x = document.getElementById(input);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
```
```{=html}
<style>
details {
background-color: lightgray;
padding: 10px;
margin: 5px;
border-radius: 5px;
}
</style>
```
APPENDIX: ggplot2 Cheatsheet{: .btn .btn-purple }{:target="_blank"}
NEXT STEPS: Earn a Workshop Badge{: .btn .btn-blue }