让图表更出彩

R 中级交互式数据可视化(plotly)

Adam Loy

Statistician, Carleton College

无需 shiny 的应用

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 步骤 1

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...