開始使用免費開始

用 ggplot2 繪製彙總後的時間序列

彙總資料能幫助你看出整體的模式與趨勢,但也可能造成資訊與脈絡的流失。不過,透過 ggplot2 的方法,你可以在視覺化時為彙總資料補回一些脈絡。

在這個練習中,你會把每週彙總的溫度讀值 weekly_avg 與原始(未彙總)的時間序列 hourly_temperature 一起繪製。hourly_temperature 代表整整 1 年、每小時取樣的溫度讀值。

時間序列 hourly_temperatureweekly_avg,以及 ggplot2zoo 套件都已為你載入可用。

本練習屬於課程

在 R 中操作時間序列資料

檢視課程

練習說明

  • 使用 ggplot() 函式,將 hourly_temperature 時間序列畫成折線圖。

  • 加上y 軸標籤 "Degrees Celsius" 與標題 "Temperature Readings"

  • 完成第二次對 geom_line()aes() 的呼叫,將 weekly_avg 時間序列覆疊到圖上。

  • 將每週彙總線條的顏色改為紅色,並將線條粗細設為 2

動手互動練習

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

# Create a plot of the hourly_temperature time series
ggplot(___, aes(___)) + 
  ___ + 
  scale_y_continuous() + 
  
  # Add axis label and title
  labs(___) + 

  # Add a line plot for the weekly aggregated time series
  geom_line(data = ___, aes(___),
            
  # Color the aggregated line in red, with a size of 2
            ___) 
編輯並執行程式碼