開始使用免費開始

加入背景文字

在前一個單元中,你已經將房價指數與實質 GDP 的動態氣泡圖潤飾完成。此練習將進一步美化圖形,讓動畫的背景顯示年份。

本練習屬於課程

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

檢視課程

練習說明

  • 將年份加入為背景文字。請把文字置中在座標點(200000450),字型顏色設為 gray80,字型大小設為 150
  • 使用 animation_slider() 隱藏滑桿。

動手互動練習

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

# Add the year as background text and remove the slider
us_economy %>%
  plot_ly(x = ~gdp, y = ~house_price, hoverinfo = "text", text = ~state) %>%
  add_text(x = ___, y = ___, text = ___, frame = ___,
           textfont = list(color = ___(___), size = ___)) %>%
  add_markers(size = ~population, color = ~region, 
              frame = ~year, ids = ~state,
              marker = list(sizemode = "diameter", sizeref = 3)) %>%
  layout(xaxis = list(title = "Real GDP (millions USD)", type = "log"),
         yaxis = list(title = "Housing price index")) %>%
  ___(___ = ___)
編輯並執行程式碼