-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass4_solutions.Rmd
86 lines (74 loc) · 2.13 KB
/
class4_solutions.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
---
title: 'Introduction to R, Class 4: Solutions'
output: github_document
---
<!--class4_solutions.md is generated from class4_solutions.Rmd. Please edit that file -->
```{r setup, include=FALSE, purl=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r include=FALSE}
library(tidyverse)
clinical <- read_csv("../data/clinical.csv")
smoke_complete <- read_csv("../data/smoke_complete.csv")
birth_reduced <- read_csv("../data/birth_reduced.csv")
```
## Challenge-scatterplot
```{r scatterplot}
ggplot(data=smoke_complete,
aes(x=age_at_diagnosis,
y=years_smoked, color=gender)) +
geom_point()
```
#### Challenge-comments
```{r comments}
# assign data and aesthetics to object
my_plot <- ggplot(smoke_complete, aes(x = vital_status, y = cigarettes_per_day))
# start with data/aesthetics object
my_plot +
# add geometry (boxplot)
geom_boxplot(outlier.shape = NA) +
# add jitter
geom_jitter(alpha = 0.2, color = "purple")
```
#### Challenge-order
Yes, the order matters.
```{r order}
ggplot(data=smoke_complete,
aes(x=vital_status, y=cigarettes_per_day)) +
geom_jitter(alpha=0.3, color="tomato") +
geom_boxplot()
```
#### Challenge-line
```{r line}
yearly_counts2 <- birth_reduced %>%
count(year_of_birth, gender)
ggplot(data=yearly_counts2,
aes(x=year_of_birth, y=n, color=gender)) +
geom_line(aes(color=gender))
```
#### Challenge-dash
```{r dash}
ggplot(data=yearly_counts2,
aes(x=year_of_birth, y=n, color=gender)) +
geom_line(aes(linetype=gender))
```
#### Challenge-panels
```{r panels}
ggplot(data=yearly_counts2,
aes(x=year_of_birth, y=n, color=gender)) +
geom_line() +
facet_wrap(~gender)
```
#### Challenge-axis
One possible search result [here](http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels#set-the-position-of-tick-marks).
```{r axis}
ggplot(data=yearly_counts2,
aes(x=year_of_birth, y=n, color=gender)) +
geom_line() +
theme(axis.text.x = element_blank(), # hide labels
axis.text.y = element_blank()) +
facet_wrap(~gender)
```
## Extra exercises
#### Challenge-improve
There are lots of options for this answer!