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(data = died_summary) + 
  geom_bar(aes(Pclass, percent_survived), stat = 'identity') +
  facet_wrap(~Sex)

  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 軸對調;使用其它的風格;或是修改與新增圖的座標軸名稱與標題等。

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

資料讀取與清理 (10 分)

# Write your code here
# 請務必印出 data frame
mydata_all <- readr::read_delim("mydata.csv", ",")
#> 
#> ─ Column specification ────────────────────────────
#> cols(
#>   `Overall rank` = col_double(),
#>   `Country or region` = col_character(),
#>   Score = col_double(),
#>   `GDP per capita` = col_double(),
#>   `Social support` = col_double(),
#>   `Healthy life expectancy` = col_double(),
#>   `Freedom to make life choices` = col_double(),
#>   Generosity = col_double(),
#>   `Perceptions of corruption` = col_double()
#> )
mydata <- data.frame(mydata_all)
mydata
#>     Overall.rank        Country.or.region Score GDP.per.capita Social.support
#> 1              1                  Finland 7.769          1.340          1.587
#> 2              2                  Denmark 7.600          1.383          1.573
#> 3              3                   Norway 7.554          1.488          1.582
#> 4              4                  Iceland 7.494          1.380          1.624
#> 5              5              Netherlands 7.488          1.396          1.522
#> 6              6              Switzerland 7.480          1.452          1.526
#> 7              7                   Sweden 7.343          1.387          1.487
#> 8              8              New Zealand 7.307          1.303          1.557
#> 9              9                   Canada 7.278          1.365          1.505
#> 10            10                  Austria 7.246          1.376          1.475
#> 11            11                Australia 7.228          1.372          1.548
#> 12            12               Costa Rica 7.167          1.034          1.441
#> 13            13                   Israel 7.139          1.276          1.455
#> 14            14               Luxembourg 7.090          1.609          1.479
#> 15            15           United Kingdom 7.054          1.333          1.538
#> 16            16                  Ireland 7.021          1.499          1.553
#> 17            17                  Germany 6.985          1.373          1.454
#> 18            18                  Belgium 6.923          1.356          1.504
#> 19            19            United States 6.892          1.433          1.457
#> 20            20           Czech Republic 6.852          1.269          1.487
#> 21            21     United Arab Emirates 6.825          1.503          1.310
#> 22            22                    Malta 6.726          1.300          1.520
#> 23            23                   Mexico 6.595          1.070          1.323
#> 24            24                   France 6.592          1.324          1.472
#> 25            25                   Taiwan 6.446          1.368          1.430
#> 26            26                    Chile 6.444          1.159          1.369
#> 27            27                Guatemala 6.436          0.800          1.269
#> 28            28             Saudi Arabia 6.375          1.403          1.357
#> 29            29                    Qatar 6.374          1.684          1.313
#> 30            30                    Spain 6.354          1.286          1.484
#> 31            31                   Panama 6.321          1.149          1.442
#> 32            32                   Brazil 6.300          1.004          1.439
#> 33            33                  Uruguay 6.293          1.124          1.465
#> 34            34                Singapore 6.262          1.572          1.463
#> 35            35              El Salvador 6.253          0.794          1.242
#> 36            36                    Italy 6.223          1.294          1.488
#> 37            37                  Bahrain 6.199          1.362          1.368
#> 38            38                 Slovakia 6.198          1.246          1.504
#> 39            39        Trinidad & Tobago 6.192          1.231          1.477
#> 40            40                   Poland 6.182          1.206          1.438
#> 41            41               Uzbekistan 6.174          0.745          1.529
#> 42            42                Lithuania 6.149          1.238          1.515
#> 43            43                 Colombia 6.125          0.985          1.410
#> 44            44                 Slovenia 6.118          1.258          1.523
#> 45            45                Nicaragua 6.105          0.694          1.325
#> 46            46                   Kosovo 6.100          0.882          1.232
#> 47            47                Argentina 6.086          1.092          1.432
#> 48            48                  Romania 6.070          1.162          1.232
#> 49            49                   Cyprus 6.046          1.263          1.223
#> 50            50                  Ecuador 6.028          0.912          1.312
#> 51            51                   Kuwait 6.021          1.500          1.319
#> 52            52                 Thailand 6.008          1.050          1.409
#> 53            53                   Latvia 5.940          1.187          1.465
#> 54            54              South Korea 5.895          1.301          1.219
#> 55            55                  Estonia 5.893          1.237          1.528
#> 56            56                  Jamaica 5.890          0.831          1.478
#> 57            57                Mauritius 5.888          1.120          1.402
#> 58            58                    Japan 5.886          1.327          1.419
#> 59            59                 Honduras 5.860          0.642          1.236
#> 60            60               Kazakhstan 5.809          1.173          1.508
#> 61            61                  Bolivia 5.779          0.776          1.209
#> 62            62                  Hungary 5.758          1.201          1.410
#> 63            63                 Paraguay 5.743          0.855          1.475
#> 64            64          Northern Cyprus 5.718          1.263          1.252
#> 65            65                     Peru 5.697          0.960          1.274
#> 66            66                 Portugal 5.693          1.221          1.431
#> 67            67                 Pakistan 5.653          0.677          0.886
#> 68            68                   Russia 5.648          1.183          1.452
#> 69            69              Philippines 5.631          0.807          1.293
#> 70            70                   Serbia 5.603          1.004          1.383
#> 71            71                  Moldova 5.529          0.685          1.328
#> 72            72                    Libya 5.525          1.044          1.303
#> 73            73               Montenegro 5.523          1.051          1.361
#> 74            74               Tajikistan 5.467          0.493          1.098
#> 75            75                  Croatia 5.432          1.155          1.266
#> 76            76                Hong Kong 5.430          1.438          1.277
#> 77            77       Dominican Republic 5.425          1.015          1.401
#> 78            78   Bosnia and Herzegovina 5.386          0.945          1.212
#> 79            79                   Turkey 5.373          1.183          1.360
#> 80            80                 Malaysia 5.339          1.221          1.171
#> 81            81                  Belarus 5.323          1.067          1.465
#> 82            82                   Greece 5.287          1.181          1.156
#> 83            83                 Mongolia 5.285          0.948          1.531
#> 84            84          North Macedonia 5.274          0.983          1.294
#> 85            85                  Nigeria 5.265          0.696          1.111
#> 86            86               Kyrgyzstan 5.261          0.551          1.438
#> 87            87             Turkmenistan 5.247          1.052          1.538
#> 88            88                  Algeria 5.211          1.002          1.160
#> 89            89                  Morocco 5.208          0.801          0.782
#> 90            90               Azerbaijan 5.208          1.043          1.147
#> 91            91                  Lebanon 5.197          0.987          1.224
#> 92            92                Indonesia 5.192          0.931          1.203
#> 93            93                    China 5.191          1.029          1.125
#> 94            94                  Vietnam 5.175          0.741          1.346
#> 95            95                   Bhutan 5.082          0.813          1.321
#> 96            96                 Cameroon 5.044          0.549          0.910
#> 97            97                 Bulgaria 5.011          1.092          1.513
#> 98            98                    Ghana 4.996          0.611          0.868
#> 99            99              Ivory Coast 4.944          0.569          0.808
#> 100          100                    Nepal 4.913          0.446          1.226
#> 101          101                   Jordan 4.906          0.837          1.225
#> 102          102                    Benin 4.883          0.393          0.437
#> 103          103      Congo (Brazzaville) 4.812          0.673          0.799
#> 104          104                    Gabon 4.799          1.057          1.183
#> 105          105                     Laos 4.796          0.764          1.030
#> 106          106             South Africa 4.722          0.960          1.351
#> 107          107                  Albania 4.719          0.947          0.848
#> 108          108                Venezuela 4.707          0.960          1.427
#> 109          109                 Cambodia 4.700          0.574          1.122
#> 110          110  Palestinian Territories 4.696          0.657          1.247
#> 111          111                  Senegal 4.681          0.450          1.134
#> 112          112                  Somalia 4.668          0.000          0.698
#> 113          113                  Namibia 4.639          0.879          1.313
#> 114          114                    Niger 4.628          0.138          0.774
#> 115          115             Burkina Faso 4.587          0.331          1.056
#> 116          116                  Armenia 4.559          0.850          1.055
#> 117          117                     Iran 4.548          1.100          0.842
#> 118          118                   Guinea 4.534          0.380          0.829
#> 119          119                  Georgia 4.519          0.886          0.666
#> 120          120                   Gambia 4.516          0.308          0.939
#> 121          121                    Kenya 4.509          0.512          0.983
#> 122          122               Mauritania 4.490          0.570          1.167
#> 123          123               Mozambique 4.466          0.204          0.986
#> 124          124                  Tunisia 4.461          0.921          1.000
#> 125          125               Bangladesh 4.456          0.562          0.928
#> 126          126                     Iraq 4.437          1.043          0.980
#> 127          127         Congo (Kinshasa) 4.418          0.094          1.125
#> 128          128                     Mali 4.390          0.385          1.105
#> 129          129             Sierra Leone 4.374          0.268          0.841
#> 130          130                Sri Lanka 4.366          0.949          1.265
#> 131          131                  Myanmar 4.360          0.710          1.181
#> 132          132                     Chad 4.350          0.350          0.766
#> 133          133                  Ukraine 4.332          0.820          1.390
#> 134          134                 Ethiopia 4.286          0.336          1.033
#> 135          135                Swaziland 4.212          0.811          1.149
#> 136          136                   Uganda 4.189          0.332          1.069
#> 137          137                    Egypt 4.166          0.913          1.039
#> 138          138                   Zambia 4.107          0.578          1.058
#> 139          139                     Togo 4.085          0.275          0.572
#> 140          140                    India 4.015          0.755          0.765
#> 141          141                  Liberia 3.975          0.073          0.922
#> 142          142                  Comoros 3.973          0.274          0.757
#> 143          143               Madagascar 3.933          0.274          0.916
#> 144          144                  Lesotho 3.802          0.489          1.169
#> 145          145                  Burundi 3.775          0.046          0.447
#> 146          146                 Zimbabwe 3.663          0.366          1.114
#> 147          147                    Haiti 3.597          0.323          0.688
#> 148          148                 Botswana 3.488          1.041          1.145
#> 149          149                    Syria 3.462          0.619          0.378
#> 150          150                   Malawi 3.410          0.191          0.560
#> 151          151                    Yemen 3.380          0.287          1.163
#> 152          152                   Rwanda 3.334          0.359          0.711
#> 153          153                 Tanzania 3.231          0.476          0.885
#> 154          154              Afghanistan 3.203          0.350          0.517
#> 155          155 Central African Republic 3.083          0.026          0.000
#> 156          156              South Sudan 2.853          0.306          0.575
#>     Healthy.life.expectancy Freedom.to.make.life.choices Generosity
#> 1                     0.986                        0.596      0.153
#> 2                     0.996                        0.592      0.252
#> 3                     1.028                        0.603      0.271
#> 4                     1.026                        0.591      0.354
#> 5                     0.999                        0.557      0.322
#> 6                     1.052                        0.572      0.263
#> 7                     1.009                        0.574      0.267
#> 8                     1.026                        0.585      0.330
#> 9                     1.039                        0.584      0.285
#> 10                    1.016                        0.532      0.244
#> 11                    1.036                        0.557      0.332
#> 12                    0.963                        0.558      0.144
#> 13                    1.029                        0.371      0.261
#> 14                    1.012                        0.526      0.194
#> 15                    0.996                        0.450      0.348
#> 16                    0.999                        0.516      0.298
#> 17                    0.987                        0.495      0.261
#> 18                    0.986                        0.473      0.160
#> 19                    0.874                        0.454      0.280
#> 20                    0.920                        0.457      0.046
#> 21                    0.825                        0.598      0.262
#> 22                    0.999                        0.564      0.375
#> 23                    0.861                        0.433      0.074
#> 24                    1.045                        0.436      0.111
#> 25                    0.914                        0.351      0.242
#> 26                    0.920                        0.357      0.187
#> 27                    0.746                        0.535      0.175
#> 28                    0.795                        0.439      0.080
#> 29                    0.871                        0.555      0.220
#> 30                    1.062                        0.362      0.153
#> 31                    0.910                        0.516      0.109
#> 32                    0.802                        0.390      0.099
#> 33                    0.891                        0.523      0.127
#> 34                    1.141                        0.556      0.271
#> 35                    0.789                        0.430      0.093
#> 36                    1.039                        0.231      0.158
#> 37                    0.871                        0.536      0.255
#> 38                    0.881                        0.334      0.121
#> 39                    0.713                        0.489      0.185
#> 40                    0.884                        0.483      0.117
#> 41                    0.756                        0.631      0.322
#> 42                    0.818                        0.291      0.043
#> 43                    0.841                        0.470      0.099
#> 44                    0.953                        0.564      0.144
#> 45                    0.835                        0.435      0.200
#> 46                    0.758                        0.489      0.262
#> 47                    0.881                        0.471      0.066
#> 48                    0.825                        0.462      0.083
#> 49                    1.042                        0.406      0.190
#> 50                    0.868                        0.498      0.126
#> 51                    0.808                        0.493      0.142
#> 52                    0.828                        0.557      0.359
#> 53                    0.812                        0.264      0.075
#> 54                    1.036                        0.159      0.175
#> 55                    0.874                        0.495      0.103
#> 56                    0.831                        0.490      0.107
#> 57                    0.798                        0.498      0.215
#> 58                    1.088                        0.445      0.069
#> 59                    0.828                        0.507      0.246
#> 60                    0.729                        0.410      0.146
#> 61                    0.706                        0.511      0.137
#> 62                    0.828                        0.199      0.081
#> 63                    0.777                        0.514      0.184
#> 64                    1.042                        0.417      0.191
#> 65                    0.854                        0.455      0.083
#> 66                    0.999                        0.508      0.047
#> 67                    0.535                        0.313      0.220
#> 68                    0.726                        0.334      0.082
#> 69                    0.657                        0.558      0.117
#> 70                    0.854                        0.282      0.137
#> 71                    0.739                        0.245      0.181
#> 72                    0.673                        0.416      0.133
#> 73                    0.871                        0.197      0.142
#> 74                    0.718                        0.389      0.230
#> 75                    0.914                        0.296      0.119
#> 76                    1.122                        0.440      0.258
#> 77                    0.779                        0.497      0.113
#> 78                    0.845                        0.212      0.263
#> 79                    0.808                        0.195      0.083
#> 80                    0.828                        0.508      0.260
#> 81                    0.789                        0.235      0.094
#> 82                    0.999                        0.067      0.000
#> 83                    0.667                        0.317      0.235
#> 84                    0.838                        0.345      0.185
#> 85                    0.245                        0.426      0.215
#> 86                    0.723                        0.508      0.300
#> 87                    0.657                        0.394      0.244
#> 88                    0.785                        0.086      0.073
#> 89                    0.782                        0.418      0.036
#> 90                    0.769                        0.351      0.035
#> 91                    0.815                        0.216      0.166
#> 92                    0.660                        0.491      0.498
#> 93                    0.893                        0.521      0.058
#> 94                    0.851                        0.543      0.147
#> 95                    0.604                        0.457      0.370
#> 96                    0.331                        0.381      0.187
#> 97                    0.815                        0.311      0.081
#> 98                    0.486                        0.381      0.245
#> 99                    0.232                        0.352      0.154
#> 100                   0.677                        0.439      0.285
#> 101                   0.815                        0.383      0.110
#> 102                   0.397                        0.349      0.175
#> 103                   0.508                        0.372      0.105
#> 104                   0.571                        0.295      0.043
#> 105                   0.551                        0.547      0.266
#> 106                   0.469                        0.389      0.130
#> 107                   0.874                        0.383      0.178
#> 108                   0.805                        0.154      0.064
#> 109                   0.637                        0.609      0.232
#> 110                   0.672                        0.225      0.103
#> 111                   0.571                        0.292      0.153
#> 112                   0.268                        0.559      0.243
#> 113                   0.477                        0.401      0.070
#> 114                   0.366                        0.318      0.188
#> 115                   0.380                        0.255      0.177
#> 116                   0.815                        0.283      0.095
#> 117                   0.785                        0.305      0.270
#> 118                   0.375                        0.332      0.207
#> 119                   0.752                        0.346      0.043
#> 120                   0.428                        0.382      0.269
#> 121                   0.581                        0.431      0.372
#> 122                   0.489                        0.066      0.106
#> 123                   0.390                        0.494      0.197
#> 124                   0.815                        0.167      0.059
#> 125                   0.723                        0.527      0.166
#> 126                   0.574                        0.241      0.148
#> 127                   0.357                        0.269      0.212
#> 128                   0.308                        0.327      0.153
#> 129                   0.242                        0.309      0.252
#> 130                   0.831                        0.470      0.244
#> 131                   0.555                        0.525      0.566
#> 132                   0.192                        0.174      0.198
#> 133                   0.739                        0.178      0.187
#> 134                   0.532                        0.344      0.209
#> 135                   0.000                        0.313      0.074
#> 136                   0.443                        0.356      0.252
#> 137                   0.644                        0.241      0.076
#> 138                   0.426                        0.431      0.247
#> 139                   0.410                        0.293      0.177
#> 140                   0.588                        0.498      0.200
#> 141                   0.443                        0.370      0.233
#> 142                   0.505                        0.142      0.275
#> 143                   0.555                        0.148      0.169
#> 144                   0.168                        0.359      0.107
#> 145                   0.380                        0.220      0.176
#> 146                   0.433                        0.361      0.151
#> 147                   0.449                        0.026      0.419
#> 148                   0.538                        0.455      0.025
#> 149                   0.440                        0.013      0.331
#> 150                   0.495                        0.443      0.218
#> 151                   0.463                        0.143      0.108
#> 152                   0.614                        0.555      0.217
#> 153                   0.499                        0.417      0.276
#> 154                   0.361                        0.000      0.158
#> 155                   0.105                        0.225      0.235
#> 156                   0.295                        0.010      0.202
#>     Perceptions.of.corruption
#> 1                       0.393
#> 2                       0.410
#> 3                       0.341
#> 4                       0.118
#> 5                       0.298
#> 6                       0.343
#> 7                       0.373
#> 8                       0.380
#> 9                       0.308
#> 10                      0.226
#> 11                      0.290
#> 12                      0.093
#> 13                      0.082
#> 14                      0.316
#> 15                      0.278
#> 16                      0.310
#> 17                      0.265
#> 18                      0.210
#> 19                      0.128
#> 20                      0.036
#> 21                      0.182
#> 22                      0.151
#> 23                      0.073
#> 24                      0.183
#> 25                      0.097
#> 26                      0.056
#> 27                      0.078
#> 28                      0.132
#> 29                      0.167
#> 30                      0.079
#> 31                      0.054
#> 32                      0.086
#> 33                      0.150
#> 34                      0.453
#> 35                      0.074
#> 36                      0.030
#> 37                      0.110
#> 38                      0.014
#> 39                      0.016
#> 40                      0.050
#> 41                      0.240
#> 42                      0.042
#> 43                      0.034
#> 44                      0.057
#> 45                      0.127
#> 46                      0.006
#> 47                      0.050
#> 48                      0.005
#> 49                      0.041
#> 50                      0.087
#> 51                      0.097
#> 52                      0.028
#> 53                      0.064
#> 54                      0.056
#> 55                      0.161
#> 56                      0.028
#> 57                      0.060
#> 58                      0.140
#> 59                      0.078
#> 60                      0.096
#> 61                      0.064
#> 62                      0.020
#> 63                      0.080
#> 64                      0.162
#> 65                      0.027
#> 66                      0.025
#> 67                      0.098
#> 68                      0.031
#> 69                      0.107
#> 70                      0.039
#> 71                      0.000
#> 72                      0.152
#> 73                      0.080
#> 74                      0.144
#> 75                      0.022
#> 76                      0.287
#> 77                      0.101
#> 78                      0.006
#> 79                      0.106
#> 80                      0.024
#> 81                      0.142
#> 82                      0.034
#> 83                      0.038
#> 84                      0.034
#> 85                      0.041
#> 86                      0.023
#> 87                      0.028
#> 88                      0.114
#> 89                      0.076
#> 90                      0.182
#> 91                      0.027
#> 92                      0.028
#> 93                      0.100
#> 94                      0.073
#> 95                      0.167
#> 96                      0.037
#> 97                      0.004
#> 98                      0.040
#> 99                      0.090
#> 100                     0.089
#> 101                     0.130
#> 102                     0.082
#> 103                     0.093
#> 104                     0.055
#> 105                     0.164
#> 106                     0.055
#> 107                     0.027
#> 108                     0.047
#> 109                     0.062
#> 110                     0.066
#> 111                     0.072
#> 112                     0.270
#> 113                     0.056
#> 114                     0.102
#> 115                     0.113
#> 116                     0.064
#> 117                     0.125
#> 118                     0.086
#> 119                     0.164
#> 120                     0.167
#> 121                     0.053
#> 122                     0.088
#> 123                     0.138
#> 124                     0.055
#> 125                     0.143
#> 126                     0.089
#> 127                     0.053
#> 128                     0.052
#> 129                     0.045
#> 130                     0.047
#> 131                     0.172
#> 132                     0.078
#> 133                     0.010
#> 134                     0.100
#> 135                     0.135
#> 136                     0.060
#> 137                     0.067
#> 138                     0.087
#> 139                     0.085
#> 140                     0.085
#> 141                     0.033
#> 142                     0.078
#> 143                     0.041
#> 144                     0.093
#> 145                     0.180
#> 146                     0.089
#> 147                     0.110
#> 148                     0.100
#> 149                     0.141
#> 150                     0.089
#> 151                     0.077
#> 152                     0.411
#> 153                     0.147
#> 154                     0.025
#> 155                     0.035
#> 156                     0.091

資料視覺化 (30 分)

# Write your code here
# 請務必印出圖片
ggplot(mydata, aes(GDP.per.capita, Generosity)) +
  geom_point(aes(color = Score)) +
  geom_smooth()
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Tweak the plot (10 分)

# Write your code here
ggplot(mydata, aes(GDP.per.capita, Generosity)) +
  geom_point(aes(color = Score)) +
  geom_smooth() +
  coord_fixed() +
  scale_color_gradientn(colors = rainbow(5))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

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 = factor(year)), method = "lm", se = FALSE) +
  geom_vline(xintercept = mean(mpg$displ)) +
  labs(x = "Engine displacement (litres)", y = "Highway miles (per gallon)") +
  guides(color = guide_legend("SUV"), linetype = guide_legend("Year"))
#> `geom_smooth()` using formula 'y ~ x'