library(faraway)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.6     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.9
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
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 ...
ggplot(aes(x = age, y = wealth, color = region), data = fortune) + 
geom_point()  + 
geom_vline(xintercept = 64.03) +
geom_hline(yintercept = 2.5)
## Warning: Removed 7 rows containing missing values (geom_point).

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, nrow = 5)
## `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).

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)
ggplot(aes(x = happy, fill = love),  data = happy) +
geom_density(alpha = 0.2)

ggplot(happy, aes(x = love, y = happy , fill = sex
 )) +
geom_point(position = "jitter") +
geom_boxplot(position = "dodge")

#XY都要是numeric,再把前面有轉factor的”love”再轉回numeric

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