運用顏色、符號與大小

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

Adam Loy

Statistician, Carleton College

以散佈圖呈現國家快樂度與預期壽命。

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

世界快樂度資料

dplyr::glimpse(happy)
Rows: 141
Columns: 11
$ country         <chr> "Afghanistan", "Albania", "Algeria", ...
$ happiness       <dbl> 2.661718, 4.639548, 5.248912, 6.039330, ...
$ region          <chr> "South Asia", "Central and Eastern Europe", ...
$ population      <dbl> 35530081, 2873457, 41318142, 44271041, ...
$ log.gdp         <dbl> 7.460144, 9.373718, 9.540244, 9.843519, ...
$ income          <fct> low, upper-middle, upper-middle, high, ...
$ life.expectancy <dbl> 52.33953, 69.05166, 65.69919, 67.53870, ...
$ social.support  <dbl> 0.4908801, 0.6376983, 0.8067539, 0.9066991, ...
$ freedom         <dbl> 0.4270109, 0.7496110, 0.4366705, 0.8319662, ...
$ generosity      <dbl> -0.106340349, -0.035140377, -0.194670126, -0.18629...
$ corruption      <dbl> 0.9543926, 0.8761346, 0.6997742, 0.8410525, ...
R 的 plotly 互動式資料視覺化:中級

圖徵顏色

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~income)

以收入類別上色的國家快樂度與預期壽命散佈圖。

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

圖徵符號

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(symbol = ~income)

國家快樂度與預期壽命散佈圖;顏色與形狀皆代表該國的收入類別。

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

以連續變數著色

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~population)

依人口數上色的國家快樂度與預期壽命散佈圖。

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

變換

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(color = ~log10(population))

以人口對數上色的國家快樂度與預期壽命散佈圖。

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

圖徵大小

happy %>%
  plot_ly(x = ~life.expectancy, y = ~happiness) %>%
  add_markers(size = ~population)

國家快樂度與預期壽命散佈圖;圖徵大小對應人口數。

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

修飾標籤

happy %>%
  plot_ly(
    x = ~life.expectancy, y = ~happiness,
    hoverinfo = "text",
    text = ~paste("Country: ", country,
                  "</br> Population: ", population)
  ) %>%
  add_markers(size = ~population) %>%
  layout(
    xaxis = list(title = "Healthy life expectancy"),
    yaxis = list(title = "National happiness score")
  )
R 的 plotly 互動式資料視覺化:中級

游標暫留互動的 GIF。

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

一起來練習吧!

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

Preparing Video For Download...