+ - 0:00:00
Notes for current slide
Notes for next slide

Interactivity

Session 10

PMAP 8921: Data Visualization with R
Andrew Young School of Policy Studies
Summer 2021

1 / 28

Plan for today

2 / 28

Plan for today

Making interactive graphics

2 / 28

Plan for today

Making interactive graphics

Sharing content

2 / 28

Making interactive graphics

3 / 28

Three general methods

Single plots with plotly

Easy!

4 / 28

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

4 / 28

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

Complete interactive apps with Shiny

Super complicated!

4 / 28

Single plots with plotly

Plotly is special software for
creating interactive plots with JavaScript

5 / 28

Single plots with plotly

Plotly is special software for
creating interactive plots with JavaScript

No knowledge of JavaScript needed!

5 / 28

Single plots with plotly

Plotly is special software for
creating interactive plots with JavaScript

No knowledge of JavaScript needed!

ggplotly() in the plotly R package translates
between R and Javascript for you!

5 / 28

Plotly

library(gapminder)
library(plotly)
gapminder_2007 <- filter(gapminder,
year == 2007)
my_plot <- ggplot(
data = gapminder_2007,
mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
geom_point() +
scale_x_log10() +
theme_minimal()
ggplotly(my_plot)
3001000300010000300004050607080
AfricaAfricaAmericasAmericasAsiaAsiaEuropeEuropeOceaniaOceaniagdpPercaplifeExpcontinent
6 / 28

Plotly tooltips

my_plot <- ggplot(
data = gapminder_2007,
mapping = aes(x = gdpPercap, y = lifeExp,
color = continent)) +
geom_point(aes(text = country)) +
scale_x_log10() +
theme_minimal()
interactive_plot <- ggplotly(
my_plot, tooltip = "text"
)
interactive_plot
3001000300010000300004050607080
AfricaAfricaAmericasAmericasAsiaAsiaEuropeEuropeOceaniaOceaniagdpPercaplifeExpcontinent
7 / 28

Works with most geoms!

car_hist <- ggplot(mpg,
aes(x = hwy)) +
geom_histogram(binwdith = 2,
boundary = 0,
color = "white")
ggplotly(car_hist)
8 / 28

Save as HTML

Save a self-contained HTML version of it with
saveWidget() in the htmlwidgets R package

# This is like ggsave, but for interactive HTML plots
htmlwidgets::saveWidget(interactive_plot, "fancy_plot.html")
9 / 28

Fully documented

The documentation for ggplot2 + plotly is full of
examples of how to customize everything

Rely on that ↑ + Google to make
really fancy (and easy!) interactive plots

10 / 28

Three general methods

Single plots with plotly

Easy!

11 / 28

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

11 / 28

Dashboards with flexdashboard

Use basic R Markdown to build a dashboard!

flexdashboard simple layout
12 / 28

Dashboards with flexdashboard

Make any kind of block arrangement

flexdashboard complex layout
13 / 28

Dashboards with flexdashboard

Add other elements like text and gauges

flexdashboard value boxes
flexdashboard gauges
14 / 28

Example dashboards

15 / 28

Example dashboards

16 / 28

Example dashboards

17 / 28

Outstanding documentation

The documentation for flexdashboard is
full of examples and details of everything you can do

Rely on that ↑ + Google to make
really fancy (and easy!) dashboards!

18 / 28

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

19 / 28

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

Complete interactive apps with Shiny

Super complicated!

19 / 28

Shiny

Shiny is a complete web application framework for interactive statistics

20 / 28

Shiny

Shiny is a complete web application framework for interactive statistics

It's super complex and hard for beginners

20 / 28

Shiny

Shiny is a complete web application framework for interactive statistics

It's super complex and hard for beginners

I've never made a standalone Shiny app!

(And I don't plan on trying anytime soon)

20 / 28

Lots of resources to help start

RStudio has a whole website for helping you get started

21 / 28

Really neat examples!

23 / 28

Really neat examples!

24 / 28

flexdashboard + Shiny

You can use reactive Shiny things in flexdashboards
without building a complete Shiny app!

I have done this

Why Donors Donate
25 / 28

Sharing content

26 / 28

What do you do after you knit?

When knitting to PDF or Word, you make a standalone file

E-mail it, message it, Slack it, whatever

27 / 28

What do you do after you knit?

When knitting to PDF or Word, you make a standalone file

E-mail it, message it, Slack it, whatever

When knitting to HTML, you make a website

27 / 28

What do you do after you knit?

When knitting to PDF or Word, you make a standalone file

E-mail it, message it, Slack it, whatever

When knitting to HTML, you make a website

By default it's a standalone .html file with graphics embedded,
so you can still e-mail it, etc., but it can get huge if there are lots of images

27 / 28

What do you do after you knit?

When knitting to PDF or Word, you make a standalone file

E-mail it, message it, Slack it, whatever

When knitting to HTML, you make a website

By default it's a standalone .html file with graphics embedded,
so you can still e-mail it, etc., but it can get huge if there are lots of images

Standalone files won't work well if there's anything interactive

27 / 28

What do you do after you knit?

When knitting to PDF or Word, you make a standalone file

E-mail it, message it, Slack it, whatever

When knitting to HTML, you make a website

By default it's a standalone .html file with graphics embedded,
so you can still e-mail it, etc., but it can get huge if there are lots of images

Standalone files won't work well if there's anything interactive

You can also post it online!

27 / 28

Places to put HTML documents

RPubs for knitted HTML documents

28 / 28

Places to put HTML documents

RPubs for knitted HTML documents

Built in to RStudio; works with ggplotly!

Publish document button
28 / 28

Places to put HTML documents

RPubs for knitted HTML documents

Built in to RStudio; works with ggplotly!

Publish document button

RPubs or shinyapps.io for flexdashboards

28 / 28

Places to put HTML documents

RPubs for knitted HTML documents

Built in to RStudio; works with ggplotly!

Publish document button

RPubs or shinyapps.io for flexdashboards

Your own web server for anything, if you have one

28 / 28

Plan for today

2 / 28
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow