1 基本題 (共 100 分)

1.1 視覺化死亡率 (共 40 分)

# 請勿更動此 code chunk 程式碼
library(dplyr)
library(ggplot2)

# 上週作業使用到的 titanic data
titanic <- readr::read_delim("titanic.csv", 
                             delim = ";", 
                             escape_double = FALSE, 
                             trim_ws = TRUE)
# 上週作業 `分組摘要` 的結果
died_summary <- titanic %>% 
  group_by(Sex, Pclass) %>%
  summarise(percent_survived = mean(Survived == "Yes")) %>%
  ungroup()
died_summary 
#> # A tibble: 6 x 3
#>   Sex    Pclass percent_survived
#>   <chr>   <dbl>            <dbl>
#> 1 female      1            0.968
#> 2 female      2            0.921
#> 3 female      3            0.5  
#> 4 male        1            0.369
#> 5 male        2            0.157
#> 6 male        3            0.135

此題延續上週作業關於鐵達尼號乘客死亡率的分組摘要。上方的程式碼即是上週分組摘要的答案,儲存於 died_summary。你的任務是使用 ggplot2 將 died_summary 繪製成此長條圖:

# Write your code here
ggplot(died_summary) +
  geom_bar(aes(x = Pclass, y = percent_survived),
           stat = "identity") +
  facet_wrap(vars(Sex))

ggsave("died_summary.png", dpi = 250)
#> Saving 7 x 5 in image
  1. 如果不知道如何下手,請先閱讀 R for Data Science 第三章
  2. 你應該會使用到 geom_bar() 或是 geom_col()
  3. 你應該會使用到 facet_wrap()
  4. 輸出結果應與下圖相同:

輸出結果應要與此圖相同

1.2 自己的資料自己畫 (共 50 分)

請自行尋找一份資料 (不得使用 titanic.csv 或內建資料),將其放在此次作業的 repo 並命名為 mydata.csv (副檔名請根據自己的資料而定, e.g., 若為 tab 分隔檔,請命名為 mydata.tsv)。你的任務是將這份資料讀入並使用 ggplot2 視覺化這份資料。

  1. (10 分) 資料讀取與清理
    將 mydata.csv 讀入並進行資料清理 (如果需要的話),以利接下來的資料視覺化

  2. (30 分) 資料視覺化
    請依這份資料的特性以及你想觀察的現象,對這份資料進行視覺化。依據你的喜好,你可以畫任意多張圖,但其中一張圖裡「必須」使用到 2 種或 2 種以上的 geom_*() 函數 (助教也只會依據這張圖評分)。這些 geom_*() 的使用需合理。例如,下方的例子雖然仍畫得出圖,但顯然是不合理的,這種情況將不予給分:

    ggplot(iris) +
      geom_bar(aes(x = Species)) +
      geom_point(aes(Sepal.Length, Petal.Width))

  3. (10 分) Tweak the plot
    請依據你的個人偏好「修改」於 2. 所繪製出來的圖。例如,你可以使用某個 coord_*() 將圖的 x、y 軸對調;使用其它的風格;或是修改與新增圖的座標軸名稱與標題等。

若覺得題目說明不夠清楚,可以參考此題的範例。

資料讀取與清理

# Write your code here
diam <- diamonds %>% 
  filter(carat < 2.5) %>% 
  sample_n(1000)

# 請務必印出 data frame
diam
#> # A tibble: 1,000 x 10
#>    carat cut       color clarity depth table price     x     y     z
#>    <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#>  1 0.99  Fair      I     SI1      60.7    66  3337  6.42  6.34  3.87
#>  2 1     Very Good E     SI1      63.1    58  4435  6.34  6.2   3.95
#>  3 1.54  Premium   I     VS1      61.6    58 10164  7.39  7.42  4.56
#>  4 0.570 Premium   D     SI2      61.3    59  1012  5.32  5.28  3.25
#>  5 0.78  Very Good F     SI2      62.4    59  2586  5.86  5.91  3.67
#>  6 0.54  Very Good H     VS2      63.2    55  1439  5.24  5.2   3.3 
#>  7 0.3   Premium   E     VS2      62.5    59   658  4.27  4.3   2.68
#>  8 1.01  Very Good E     VS1      62      57  6996  6.46  6.38  3.98
#>  9 0.4   Ideal     E     VS1      59.2    62  1180  4.8   4.83  2.85
#> 10 0.73  Ideal     F     VVS2     62.2    54  3193  5.8   5.75  3.59
#> # … with 990 more rows

資料視覺化

# Write your code here
pl <- ggplot(diam) +
  geom_histogram(aes(x = carat), alpha = 0.6, binwidth = 0.1) +
  geom_freqpoly(aes(carat, color = cut), binwidth = 0.1)

# 請務必印出圖片
pl

Tweak the plot

# Write your code here
pl + theme_bw() +
  labs(title = "Diamond Weight Distribution")

1.3 線上實習課調查問卷 (10 分)

請填寫此份課程調查問卷,並將下方 我,未命名,已完成問卷填答 內的 未命名 更改為自己的姓名:

我,未命名,已完成問卷填答

2 進階選答題 (共 20 分)

請使用 ggplot2 中的 mpg 這份資料繪製圖表。 (可使用 ?mpg 查看這份資料的說明)

  1. 請以顏色標示「車種」 class 是否為 SUV。 (6分)
  2. 請繪製「引擎排氣量」 displ 和「每加侖可高速行駛英里」 hwy 的線性回歸線,並將「年分」 year 以不同線條類型標示,且不須繪製信心區間 (請使用 geom_smooth())。(6分)
  3. 請以黑色直線標示「引擎排氣量」 displ 的平均值。(6分)
  4. 請將顏色和線條類型的圖例名稱分別設定為 SUV 和 Year 。(2分)
  • 本題輸出結果應如下圖:

進階題輸出結果

# Modify the code below
ggplot(data = mpg, mapping = aes(displ, hwy)) +
  geom_point(aes(color = class == "suv")) +
  geom_smooth(aes(linetype = as.character(year)), method = "lm", se = FALSE) +
  geom_vline(aes(xintercept = mean(displ))) +
  labs(x = "Engine displacement (litres)", y = "Highway miles (per gallon)", color = "SUV", linetype = "Year")
#> `geom_smooth()` using formula 'y ~ x'