製作更吸睛的圖表

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

Adam Loy

Statistician, Carleton College

不靠 shiny 也能做 App

bscols 範例動畫

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

用 bscols() 排版成欄

library(plotly)
library(crosstalk)
shared_data <- world2014 %>% SharedData$new()

p1 <- shared_data %>% plot_ly(x=~income, y = ~co2, color = ~four_regions) %>% add_markers() %>% layout(xaxis = list(type = "log"), yaxis = list(type = "log")) p2 <- shared_data %>% plot_ly(x=~income, y = ~life_expectancy, color = ~four_regions) %>% add_markers() %>% layout(xaxis = list(type = "log")))
bscols(p1, p2)
R 的 plotly 互動式資料視覺化:中級

用 bscols() 排版成欄

bscols 第一步

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

加入篩選:核取方塊

核取方塊篩選

bscols(filter_checkbox(id = "four_regions", label = "Region", 
                       sharedData = shared_data, group = ~four_regions),
       p1)
R 的 plotly 互動式資料視覺化:中級

加入篩選:下拉選單

下拉選單篩選

bscols(filter_select(id = "four_regions", label = "Region", 
                sharedData = shared_data, group = ~four_regions),
       p1)
R 的 plotly 互動式資料視覺化:中級

加入篩選:滑桿

滑桿篩選

bscols(filter_slider(id = "co2", label = "CO2 concentrations", 
                     sharedData = shared_data, column = ~co2),
       p1)
R 的 plotly 互動式資料視覺化:中級

固定座標軸範圍

固定座標範圍

bscols(filter_slider(id = "co2", label = "CO2 concentrations", 
                     sharedData = shared_data, column = ~co2),
        p1 %>% layout(xaxis = list(range = c(2.5, 5)), 
                      yaxis = list(range = c(-1.4, 1.55)))
)
R 的 plotly 互動式資料視覺化:中級

整合各元件

bscols(widths = c(2, 5, 5),
  list(
    filter_checkbox(
      id = "four_regions", label = "Region", 
      sharedData = shared_data, group = ~four_regions
    ),
    filter_slider(
      id = "co2", label = "CO2 concentrations", 
      sharedData = shared_data, column = ~co2
    )
  ),
  p1,
  p2
)
R 的 plotly 互動式資料視覺化:中級

整合各元件

整合後結果(靜態)

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

一起來練習吧!

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

Preparing Video For Download...