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, color = region), data = fortune) + 
 geom_point()  + 
  geom_vline(xintercept = 64.03 )  + geom_hline(aes(yintercept = 3)) + theme_bw()
## Warning: Removed 7 rows containing missing values (geom_point).

# 其他嘗試
ggplot(aes(x = age, y = wealth, color = region), data = fortune) + 
 geom_point()  + 
  geom_vline(xintercept = 64.03 )  + geom_hline(aes(yintercept = 3))
## 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, 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).

# 其他嘗試
ggplot(aes(x = age, y = wealth, group =region), data = fortune) + geom_line(mapping = aes(group = region), color = "gray70") +
  geom_point(aes(color = region)) 
## Warning: Removed 7 row(s) containing missing values (geom_path).
## 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(data = filter(fortune, region == "E"), se = FALSE) + geom_smooth(data = filter(fortune, region == "A"), se = FALSE) + geom_smooth(data = filter(fortune, region == "M"), se = FALSE) + geom_smooth(data = filter(fortune, region == "O"), se = FALSE) + geom_smooth(data = filter(fortune, region == "U"), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 4 rows containing non-finite values (stat_smooth).
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 7 rows containing missing values (geom_point).

Assessment 3

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

# 其他的嘗試
ggplot(aes(x = happy),  data = happy) +
    geom_density(aes(color = love, fill = love))

Assessment 4

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

# 其他嘗試
ggplot(happy, aes(x = love, y = happy, fill = sex)) +
  geom_point(position = "jitter") +
  geom_boxplot()+ coord_flip()

Asseeement 5

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  : Factor w/ 2 levels "0","1": 1 2 1 2 2 2 2 1 2 2 ...
##  $ love : Factor w/ 3 levels "1","2","3": 3 3 3 3 1 3 3 2 2 1 ...
##  $ work : num  4 1 5 3 2 4 4 3 2 4 ...
ggplot(happy, aes(x = money, y = happy, color = sex)) + geom_smooth(method = lm, se = F, fullrange = T) + geom_point(position = "jitter") + 
  theme_bw()
## `geom_smooth()` using formula 'y ~ x'

x軸若是category或是factor(例如:在這裡的love是factor),所以畫不出回歸線,要畫出回歸線必須要將x軸變成numeric(更換為money)。 而在顏色的部分,也需要將作為分隔的轉換為factor,才能畫出回歸線。(但其實在這個資料中sex已經是factor,所以不另外轉一個factor也可以)

# 其他嘗試
ggplot(data = happy, aes(x = happy, y = money, color = sex)) +
  geom_point(position = "jitter") + 
  geom_smooth(method=lm, se=FALSE, color = "brown", fullrange=TRUE) 
## `geom_smooth()` using formula 'y ~ x'