library(faraway)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
data
data(fortune)
head(fortune)
## wealth age region
## 1 37.0 50 M
## 2 24.0 88 U
## 3 14.0 64 A
## 4 13.0 63 U
## 5 13.0 66 U
## 6 11.7 72 E
str(fortune)
## 'data.frame': 232 obs. of 3 variables:
## $ wealth: num 37 24 14 13 13 11.7 10 8.2 8.1 7.2 ...
## $ age : int 50 88 64 63 66 72 71 77 68 66 ...
## $ region: Factor w/ 5 levels "A","E","M","O",..: 3 5 1 5 5 2 3 5 5 2 ...
Assessment 1
ggplot(aes(x = age, y = wealth), data = fortune) +
geom_point(aes(color = region)) +
geom_vline(xintercept = 64.03) +
geom_hline(yintercept = 2.61) +
theme_bw()
## Warning: Removed 7 rows containing missing values (geom_point).

Assessment 2
ggplot(aes(x = age, y = wealth, group = region), data = fortune) +
geom_point(aes(color = region)) +
geom_smooth(aes(color = region), se=FALSE)+
facet_wrap(~ region, ncol = 1) +
theme_bw()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 7 rows containing non-finite values (stat_smooth).
## Warning: Removed 7 rows containing missing values (geom_point).

another data
data(happy)
str(happy)
## 'data.frame': 39 obs. of 5 variables:
## $ happy: num 10 8 8 8 4 9 8 6 5 4 ...
## $ money: num 36 47 53 35 88 175 175 45 35 55 ...
## $ sex : num 0 1 0 1 1 1 1 0 1 1 ...
## $ love : num 3 3 3 3 1 3 3 2 2 1 ...
## $ work : num 4 1 5 3 2 4 4 3 2 4 ...
happy$sex <- as.factor(happy$sex)
happy$love <- as.factor(happy$love)
Assessment 3
ggplot(aes(x = happy), data = happy) +
geom_density(aes(fill = love), alpha = 0.4)

Assessment 4
ggplot(happy, aes(x = love, y = happy, fill = sex)) +
geom_point(position = "jitter") +
geom_boxplot(alpha = 1)

Assessment 5
happy$sex <- as.factor(happy$sex)
happy$love <- as.numeric(happy$love)
ggplot(happy, aes(x = love, y = happy, color = sex)) +
geom_point(position = "jitter") +
geom_smooth(method = lm) +
theme_bw()
## `geom_smooth()` using formula 'y ~ x'
