刷选分组

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

Adam Loy

Statistician, Carleton College

世界指标

world_indicators
# A tibble: 11,387 x 11
  country  year income     co2 military population  urban life_expectancy four_regions
  <chr>   <dbl>  <dbl>   <dbl>    <dbl>      <dbl>  <dbl>           <dbl> <chr>       
1 Afghan…  1960   1210  0.0461       NA    9000000 7.56e5            38.6 asia        
2 Albania  1960   2790  1.24         NA    1640000 4.94e5            62.7 europe      
3 Algeria  1960   6520  0.554        NA   11100000 3.39e6            52   africa      
4 Andorra  1960  15200 NA            NA      13400 7.84e3            NA   europe      
5 Angola   1960   3860  0.0975       NA    5640000 5.89e5            42.4 africa      
6 Antigu…  1960   4420  0.663        NA      55300 2.19e4            62.9 americas    
# … with 1.138e+04 more rows, and 2 more variables: eight_regions <chr>,
#   six_regions <chr>
R 中级交互式数据可视化(plotly)

选择单条时间序列

默认时间序列

world_indicators %>%
  plot_ly(x = ~year, y = ~income, alpha = 0.5) %>%
  group_by(country) %>%
  add_lines()
R 中级交互式数据可视化(plotly)

选择单条时间序列

选择时间序列动图

创建带键的 SharedData 对象

world_indicators %>%
  SharedData$new(key = ~country) %>% 
  plot_ly(x = ~year, y = ~income, alpha = 0.5) %>%
  group_by(country) %>%
  add_lines()
R 中级交互式数据可视化(plotly)

在散点图上选择分组

在散点图上选择分组动图

world_indicators %>%
  filter(year == 2014) %>%
  SharedData$new(~six_regions) %>%
  plot_ly(x=~military, y = ~co2, text = ~country) %>%
  add_markers()
R 中级交互式数据可视化(plotly)

联动汇总视图与明细视图

链接汇总与明细的散点图动图

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

联动汇总视图与明细视图

shared_data <- world_indicators %>% 
  filter(year == 2014) %>% 
  SharedData$new(key = ~six_regions)

p1 <- shared_data %>% plot_ly() %>% group_by(six_regions) %>% summarize(avg.military = mean(military, na.rm = TRUE)) %>% add_markers(x = ~avg.military, y = ~six_regions)
p2 <- shared_data %>% plot_ly(x=~military, y = ~co2, text = ~country) %>% add_markers()
subplot(p1, p2) %>% hide_legend()
R 中级交互式数据可视化(plotly)

链接汇总与明细的散点图动图

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

开始练习!

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

Preparing Video For Download...