開始使用免費開始

繪製基準線

你可以像在 ggplot2 一樣,在 plotly 中為不同圖層使用不同的資料集。當你想在動畫中顯示基準時間點(或群組)時,這是一個很實用的策略。在這個練習中,你要建立一個以房價指數對實質 GDP 的動畫散佈圖,同時把 1997 年的資料點保留在背景。

本練習屬於課程

R 的 plotly 互動式資料視覺化:中級

檢視課程

練習說明

  • 先加入代表 1997 年資料(在 us1997 中)的靜態點作為第一層。將這些點的 color 設為 "gray60"opacity 設為 0.5
  • 加入第二個 trace,使用完整的 us_economy 資料框,依時間(year)建立動畫散佈圖。記住,每個點在任一給定的 year 都代表一個 state

動手互動練習

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

# extract the 1997 data
us1997 <- us_economy %>%
  filter(year == 1997)

# create an animated scatterplot with baseline from 1997
us_economy %>%
  plot_ly(x = ~gdp, y = ~house_price) %>%
  add_markers(data = ___, marker = list(color = ___, opacity = ___)) %>%
  add_markers(frame = ___, ids = ___, data = us_economy, showlegend = FALSE, alpha = 0.5) %>%
  layout(xaxis = list(type = "log"))
編輯並執行程式碼