Bivariate graphics

Interactive Data Visualization with plotly in R

Adam Loy

Statistician, Carleton College

Wine quality data

glimpse(winequality)
Rows: 178
Columns: 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", ...
Interactive Data Visualization with plotly in R

Scatterplots with plotly

13_scatter.gif

winequality %>%
  plot_ly(x = ~residual_sugar, y = ~fixed_acidity) %>%
  add_markers()
Interactive Data Visualization with plotly in R

Stacked bar charts with plotly

13_stacked_bar.gif

winequality %>%
  count(type, quality_label) %>%

plot_ly(x = ~type, y = ~n, color = ~quality_label) %>%
add_bars() %>%
layout(barmode = "stack")
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
Interactive Data Visualization with plotly in R

Boxplots with plotly

13_boxplots.gif

winequality %>%
  plot_ly(x = ~quality_label, y = ~alcohol) %>%
  add_boxplot()
Interactive Data Visualization with plotly in R

Let's practice!

Interactive Data Visualization with plotly in R

Preparing Video For Download...