開始使用免費開始

繪製時間序列折線圖

在上一章中,你學會如何用 summarize() 依年份彙整投票資料集,特別是每一年「yes」投票的百分比。

現在你將使用 ggplot2 套件,把結果轉成一個呈現「yes」投票百分比隨時間變化的視覺化。

本練習屬於課程

案例研究:R 中的探索性資料分析

檢視課程

練習說明

by_year 資料集包含每年的投票數量與「yes」投票百分比。

  • 載入 ggplot2 套件。
  • 使用 ggplot() 並搭配 geom_line 圖層,建立一張以 year 為 x 軸、percent_yes 為 y 軸的折線圖。

動手互動練習

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

# Define by_year
by_year <- votes_processed %>%
  group_by(year) %>%
  summarize(total = n(),
            percent_yes = mean(vote == 1))

# Load the ggplot2 package


# Create line plot
___(by_year, ___) ___
  ___
編輯並執行程式碼