library(faraway)
library(tidyverse)

匯入資料

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, alpha = region, color = region), data = fortune) + 
 geom_point()  + 
  geom_vline(xintercept = 64.03) +geom_hline(yintercept = mean(fortune$wealth))
## Warning: Using alpha for a discrete variable is not advised.
## Warning: Removed 7 rows containing missing values (geom_point).

Assessment 2

ggplot(aes(x = age, y = wealth, linetype = region), data = fortune) + 
  geom_point(aes(color = region)) +
   geom_smooth(aes(color = region)) +facet_wrap(~ region, nrow = 5)

匯入資料

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, data=sex, fill=sex)) +
  geom_point(position = "jitter") +
  geom_boxplot(position = "dodge")

Assessment 5

老師真的不會了…