forked from fels-bioinformatics/fels_bioinformatics_meetup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week3_practice_plotting_with_ggplot_ANSWERS.Rmd
106 lines (73 loc) · 2.24 KB
/
week3_practice_plotting_with_ggplot_ANSWERS.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: 'Week 3 Practice: Plotting with ggplot'
output:
word_document: default
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
# load libraries
library(tidyverse)
library(conflicted)
library(viridis)
# configure knit settings
knitr::opts_chunk$set(echo = TRUE)
# resolve package conflicts
filter <- dplyr::filter
select <- dplyr::select
```
# Plotting with ggplot
We'll be using the diamonds dataset for practice this week. It comes with the dplyr package, so make sure to run the set up chunk above before doing anything else.
## Review the basics
Look at the dataset before you do anything else. You'll need to know the column names and types before plotting
```{r}
head(diamonds)
```
Plot a scatterplot with depth on the x-axis and table on the y-axis.
```{r}
ggplot(diamonds, aes(x = depth, y = table)) + geom_point()
```
Now, with the same variables, add color by cut.
```{r}
ggplot(diamonds, aes(x = depth, y = table, color = cut)) + geom_point()
```
Make boxplots for prices at each cut.
```{r}
ggplot(diamonds, aes(x = cut, y = price)) + geom_boxplot()
```
Make a barplot for diamond color.
```{r}
ggplot(diamonds, aes(x = color)) + geom_bar()
```
## Free Plotting
Plot the columns listed using whatever type of plot and any extras you'd like.
All the answers in this section are guidelines; as long as you got a plot out, you did it.
---
price
```{r}
ggplot(diamonds, aes(x = price)) + geom_histogram()
```
carat, clarity
```{r}
ggplot(diamonds, aes(x = carat, fill = clarity)) + geom_density(alpha = 0.5)
```
cut, price
```{r}
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin(alpha = 0.8)
```
cut, color, price
```{r}
ggplot(diamonds, aes(x = color, y = price, fill = color)) + geom_boxplot(alpha = 0.8) + facet_grid(~ cut)
```
## Challenge Questions
Make a scatter plot for price vs. carat, colored with viridis by depth, and with partial transparency.
```{r}
ggplot(diamonds, aes(x = price, y = carat, color = depth)) + geom_point(alpha = 0.5) + scale_color_viridis()
```
Make density plots for price, faceted by clarity and filled by clarity using viridis.
```{r}
ggplot(diamonds, aes(x = price, fill = clarity)) +
geom_density() +
scale_fill_viridis_d() +
facet_wrap(~ clarity)
```