開始使用免費開始

各洲奧運獎牌數/參賽者

你想比較各洲選手在歷屆奧運(含冬季與夏季)中的表現。你拿到一個資料集 medal_df,內容是自 1928 年起,各洲「每位參賽者平均獲得的獎牌數」。你將把資料補齊,在某洲某年沒有獎牌的情況下,以 0 表示。

Olympic flag

已為你預先載入 ggplot2 套件。

本練習屬於課程

使用 tidyr 重塑資料

檢視課程

練習說明

  • 補齊資料集,讓每個 continent 在每一屆奧運都有 medals_per_participant 值。遺漏值請以 0 補上。
  • 使用 nesting() 函式巢狀 seasonyear 變數,因為夏季奧運與冬季奧運並非同一年舉行。
  • 使用 ggplot() 繪製折線圖,將 year 對應到 medals_per_participant,並以 continent 分色。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

medal_df %>% 
  # Give each continent an observation at each Olympic event
  complete(
    ___, 
    ___, 
    fill = ___(___ = ___)
  ) %>%
  # Plot the medals_per_participant over time, colored by continent
  ggplot(___) +
  ___ +
  facet_grid(season ~ .)
編輯並執行程式碼