An Introduction to plotly IN TERACTIVE DATA VIS UALIZ ATION W ITH - - PowerPoint PPT Presentation

an introduction to plotly
SMART_READER_LITE
LIVE PREVIEW

An Introduction to plotly IN TERACTIVE DATA VIS UALIZ ATION W ITH - - PowerPoint PPT Presentation

An Introduction to plotly IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R Adam Loy Statistician, Carleton College plotly Visualization library for interactive and dynamic web-based graphics Plots work in multiple formats viewer


slide-1
SLIDE 1

An Introduction to plotly

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R

Adam Loy

Statistician, Carleton College

slide-2
SLIDE 2

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

plotly

Visualization library for interactive and dynamic web-based graphics Plots work in multiple formats viewer windows R Markdown documents shiny apps Active development + supportive community

slide-3
SLIDE 3

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Static vs. Interactive graphics

slide-4
SLIDE 4

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Wine data

library(dplyr) glimpse(wine) Observations: 178 Variables: 14 $ Type <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1... $ Alcohol <dbl> 14.23, 13.20, 13.16, 14.37, 13.24, 14.20, 14.3... $ Malic <dbl> 1.71, 1.78, 2.36, 1.95, 2.59, 1.76, 1.87, 2.15... ... $ Hue <dbl> 1.04, 1.05, 1.03, 0.86, 1.04, 1.05, 1.02, 1.06... $ Dilution <dbl> 3.92, 3.40, 3.17, 3.45, 2.93, 2.85, 3.58, 3.58... $ Proline <int> 1065, 1050, 1185, 1480, 735, 1450, 1290, 1295,...

slide-5
SLIDE 5

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

ggplot2 scatterplot

library(ggplot2) static <- wine %>% ggplot(aes(x = Flavanoids, y = Proline, color = Type)) + geom_point()

Dataset, wine Aesthetics, aes() Add a layer,

geom_point()

slide-6
SLIDE 6

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

ggplotly()

library(plotly) ggplotly(static)

slide-7
SLIDE 7

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Remarks

Interactive ? Good Bad design = bad interactive graphic Follow data-viz best practices ggplotly() is only the beginning

slide-8
SLIDE 8

Let's practice!

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R

slide-9
SLIDE 9

Plotting a single variable

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R

Adam Loy

Statistician, Carleton College

slide-10
SLIDE 10

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Exploring the wine data

library(dplyr) glimpse(wine) Observations: 178 Variables: 14 $ Type <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1... $ Alcohol <dbl> 14.23, 13.20, 13.16, 14.37, 13.24, 14.20, 14.3... $ Malic <dbl> 1.71, 1.78, 2.36, 1.95, 2.59, 1.76, 1.87, 2.15... ... $ Hue <dbl> 1.04, 1.05, 1.03, 0.86, 1.04, 1.05, 1.02, 1.06... $ Dilution <dbl> 3.92, 3.40, 3.17, 3.45, 2.93, 2.85, 3.58, 3.58... $ Proline <int> 1065, 1050, 1185, 1480, 735, 1450, 1290, 1295,...

slide-11
SLIDE 11

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Bar charts with plotly

library(plotly) wine %>% count(Type) %>% plot_ly(x = ~Type, y = ~n) %>% add_bars()

Create a frequency table with count() Specify aesthetics using ~ Add the bars trace with add_bars()

slide-12
SLIDE 12

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Reordering the bars

library(forcats) wine %>% count(Type) %>% mutate(Type = fct_reorder(Type, n, .desc = TRUE)) %>% plot_ly(x = ~Type, y = ~n) %>% add_bars() fct_reorder() to

rearrange the bars set .desc argument to

TRUE

slide-13
SLIDE 13

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Histograms with plotly

wine %>% plot_ly(x = ~Phenols) %>% # specify aesthetics add_histogram() # add the histogram trace

slide-14
SLIDE 14

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Adjusting the number of bins

wine %>% plot_ly(x = ~Phenols) %>% add_histogram(nbinsx = 10)

slide-15
SLIDE 15

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Adjusting the bin width

wine %>% plot_ly(x = ~Phenols) %>% add_histogram(xbins = list(start = 0.8, end = 4, size = 0.25))

slide-16
SLIDE 16

Let's practice!

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R

slide-17
SLIDE 17

Bivariate graphics

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R

Adam Loy

Statistician, Carleton College

slide-18
SLIDE 18

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Wine quality data

glimpse(winequality) Observations: 325 Variables: 14 $ type <chr> "red", "red", "red", "red", "red", "red", ... $ fixed_acidity <dbl> 8.2, 8.2, 8.0, 10.2, 8.6, 6.1, 10.7, 9.1, 7.2... $ volatile_acidity <dbl> 0.885, 0.640, 0.715, 0.360, 0.520, 0.590, 0.6... $ citric_acid <dbl> 0.20, 0.27, 0.22, 0.64, 0.38, 0.01, 0.22, 0.3... $ residual_sugar <dbl> 1.40, 2.00, 2.30, 2.90, 1.50, 2.10, 2.70, 2.1... ... $ sulphates <dbl> 0.46, 0.62, 0.54, 0.66, 0.52, 0.56, 0.98, 0.8... $ alcohol <dbl> 10.0, 9.1, 9.5, 12.5, 9.4, 11.4, 9.9, 11.2, 1... $ quality <int> 5, 6, 6, 6, 5, 5, 6, 6, 6, 7, 6, 5, 4, 6, 6, ... $ quality_label <chr> "low", "medium", "medium", "medium", "low", ...

slide-19
SLIDE 19

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Scatterplots with plotly

winequality %>% plot_ly(x = ~residual_sugar, y = ~fixed_acidity) %>% add_markers()

slide-20
SLIDE 20

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Stacked bar charts with plotly

winequality %>% count(type, quality_label) %>% plot_ly(x = ~type, y = ~n, color = ~quality_label) %>% add_bars() %>% layout(barmode = "stack")

slide-21
SLIDE 21

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

From counts to proportions

winequality %>% count(type, quality_label) %>% group_by(type) %>% mutate(prop = n / sum(n)) %>% plot_ly(x = ~type, y = ~prop, color = ~quality_label) %>% add_bars() %>% layout(barmode = "stack")

Group the table with

group_by()

Calculate the proportions

slide-22
SLIDE 22

INTERACTIVE DATA VISUALIZATION WITH PLOTLY IN R

Boxplots with plotly

winequality %>% plot_ly(x = ~quality_label, y = ~alcohol) %>% add_boxplot()

slide-23
SLIDE 23

Let's practice!

IN TERACTIVE DATA VIS UALIZ ATION W ITH P LOTLY IN R