Linking t w o charts IN TE R ME D IATE IN TE R AC TIVE DATA VISU - - PowerPoint PPT Presentation

linking t w o charts
SMART_READER_LITE
LIVE PREVIEW

Linking t w o charts IN TE R ME D IATE IN TE R AC TIVE DATA VISU - - PowerPoint PPT Presentation

Linking t w o charts IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R Adam Lo y Statistician , Carleton College E x ploring cl u sters INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R E x ploring longit u


slide-1
SLIDE 1

Linking two charts

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

Adam Loy

Statistician, Carleton College

slide-2
SLIDE 2

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Exploring clusters

slide-3
SLIDE 3

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Exploring longitudinal data

slide-4
SLIDE 4

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Crosstalk

Enables linked plots via JavaScript Creates static HTML les that you can easily host Displays in the RStudio viewer pane

slide-5
SLIDE 5

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

2014 world indicators

world2014 # A tibble: 193 x 11 country year income co2 military population urban life_expectancy four_regions <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> 1 Afghan… 2014 1780 0.299 1.3 32800000 8.05e6 57.8 asia 2 Albania 2014 10700 1.96 1.35 2920000 1.63e6 77.4 europe 3 Algeria 2014 13500 3.72 5.55 39100000 2.75e7 77.1 africa 4 Andorra 2014 44900 5.83 NA 79200 7.01e4 82.6 europe 5 Angola 2014 6260 1.29 4.7 26900000 1.69e7 63.3 africa 6 Antigu… 2014 19500 5.38 NA 98900 2.49e4 77.1 americas # … with 187 more rows, and 2 more variables: eight_regions <chr>, six_regions <chr>

slide-6
SLIDE 6

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Separate views

p1 <- world2014 %>% plot_ly(x = ~income, y = ~co2) %>% add_markers() p2 <- world2014 %>% plot_ly(x = ~military, y = ~co2) %>% add_markers() subplot(p1, p2, titleX = TRUE, titleY = TRUE) %>% hide_legend()

slide-7
SLIDE 7

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Linked views

slide-8
SLIDE 8

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Linked views

library(crosstalk) shared_data <- SharedData$new(world2014) p1 <- shared_data %>% plot_ly(x = ~income, y = ~co2) %>% add_markers() p2 <- shared_data %>% plot_ly(x = ~military, y = ~co2) %>% add_markers() subplot(p1, p2, titleX = TRUE, titleY = TRUE) %>% hide_legend()

slide-9
SLIDE 9

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Linked brushing

Enable linked brushing via highlight()

subplot(p1, p2, titleX = TRUE, titleY = TRUE) %>% hide_legend() %>% highlight(on = "plotly_selected")

slide-10
SLIDE 10

Let's practice!

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

slide-11
SLIDE 11

Brushing groups

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

Adam Loy

Statistician, Carleton College

slide-12
SLIDE 12

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

World indicators

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>

slide-13
SLIDE 13

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Selecting individual time series

world_indicators %>% plot_ly(x = ~year, y = ~income, alpha = 0.5) %>% group_by(country) %>% add_lines()

slide-14
SLIDE 14

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Selecting individual time series

Create a SharedData object with a key

world_indicators %>% SharedData$new(key = ~country) %>% plot_ly(x = ~year, y = ~income, alpha = 0.5) %>% group_by(country) %>% add_lines()

slide-15
SLIDE 15

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Selecting groups on a scatterplot

world_indicators %>% filter(year == 2014) %>% SharedData$new(~six_regions) %>% plot_ly(x=~military, y = ~co2, text = ~country) %>% add_markers()

slide-16
SLIDE 16

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Linking a summary and detailed view

slide-17
SLIDE 17

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Linking a summary and detailed view

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()

slide-18
SLIDE 18

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

slide-19
SLIDE 19

Let's practice!

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

slide-20
SLIDE 20

Selection strategies

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

Adam Loy

Statistician, Carleton College

slide-21
SLIDE 21

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Two selection strategies

Transient selection previously selected cases are forgoen Persistent selection selected cases accumulate

slide-22
SLIDE 22

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Example

slide-23
SLIDE 23

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Generate the base chart

shared_data <- world2014 %>% SharedData$new() p1 <- shared_data %>% plot_ly(x=~urban/population, y = ~co2, text = ~country) %>% add_markers() p2 <- shared_data %>% plot_ly(x=~income, y = ~co2, text = ~country) %>% add_markers() subplot(p1, p2, titleX = TRUE, shareY = TRUE) %>% hide_legend()

slide-24
SLIDE 24

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Persistent selection

Activate persistent selection via highlight()

subplot(p1, p2, titleX = TRUE, shareY = TRUE) %>% hide_legend() %>% highlight(persistent = TRUE)

slide-25
SLIDE 25

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Highlighting in color

slide-26
SLIDE 26

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Highlighting in color

Add dynamic = TRUE to activate a color picker

subplot(p1, p2, titleX = TRUE, shareY = TRUE) %>% hide_legend() %>% highlight(persistent = TRUE, dynamic = TRUE)

slide-27
SLIDE 27

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Two manipulation types

Direct manipulation selection performed by interacting with the graphical elements Indirect manipulation selection performed via query outside of the chart

slide-28
SLIDE 28

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

slide-29
SLIDE 29

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Indirect manipulation

world_indicators %>% SharedData$new(key = ~country) %>% plot_ly(x = ~year, y = ~income, alpha = 0.5) %>% group_by(country) %>% add_lines()

slide-30
SLIDE 30

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Indirect manipulation

world_indicators %>% SharedData$new(key = ~country, group = "Select a country") %>% plot_ly(x = ~year, y = ~income, alpha = 0.5) %>% group_by(country) %>% add_lines() %>% highlight(selectize = TRUE)

slide-31
SLIDE 31

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

slide-32
SLIDE 32

Let's practice!

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

slide-33
SLIDE 33

Making shinier charts

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R

Adam Loy

Statistician, Carleton College

slide-34
SLIDE 34

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Apps without shiny

slide-35
SLIDE 35

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

bscols() for column layouts

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)

slide-36
SLIDE 36

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

bscols() for column layouts

slide-37
SLIDE 37

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Adding filters: Checkboxes

bscols(filter_checkbox(id = "four_regions", label = "Region", sharedData = shared_data, group = ~four_regions), p1)

slide-38
SLIDE 38

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Adding filters: Select box

bscols(filter_select(id = "four_regions", label = "Region", sharedData = shared_data, group = ~four_regions), p1)

slide-39
SLIDE 39

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Adding filters: Sliders

bscols(filter_slider(id = "co2", label = "CO2 concentrations", sharedData = shared_data, column = ~co2), p1)

slide-40
SLIDE 40

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Fixing the range of your axes

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

slide-41
SLIDE 41

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Putting the pieces together

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 )

slide-42
SLIDE 42

INTERMEDIATE INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Putting the pieces together

slide-43
SLIDE 43

Let's practice!

IN TE R ME D IATE IN TE R AC TIVE DATA VISU AL IZATION W ITH P L OTLY IN R