開始使用免費開始

潤飾你的圖形

下方的程式碼會繪製一張互動式圖表,將各國的整體幸福度與社會支持指數對照,並以點的符號表示國家的收入分類。

happy %>%
  plot_ly(x = ~social.support, y = ~happiness,
  hoverinfo = "text",
  text = ~paste("Country: ", country)) %>%
  add_markers(symbol = ~income, symbols = c("circle-open", "square-open", "star-open", "x-thin-open"))

你的任務是編修滑鼠暫留資訊與座標軸標籤,讓讀者更容易理解圖中的資訊。

plotly 套件與 happy 資料集已為你載入。

本練習屬於課程

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

檢視課程

練習說明

  • 在滑鼠暫留資訊中加入 income 所屬群組、happiness 分數,以及 social.support 指數。
  • 使用 round(<variable>, 2) 將所有數值變數在暫留資訊中四捨五入到小數點後 2 位。
  • 將 x 軸標籤改為 "Social support index",y 軸標籤改為 "National happiness score"

動手互動練習

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

# Complete the following code to polish the plot
happy %>%
  plot_ly(x = ~social.support, y = ~happiness,
  hoverinfo = "text",
  text = ~paste("Country: ", country,
  "
Income: ", ___, "
Happiness: ", ___, "
Social support: ", ___)) %>% add_markers(symbol = ~income, symbols = c("circle-open", "square-open", "star-open", "x-thin-open")) %>% layout(xaxis = ___, yaxis = ___)
編輯並執行程式碼