Interactivity Session 10 PMAP 8921: Data Visualization with R - - PowerPoint PPT Presentation

interactivity
SMART_READER_LITE
LIVE PREVIEW

Interactivity Session 10 PMAP 8921: Data Visualization with R - - PowerPoint PPT Presentation

Interactivity Session 10 PMAP 8921: Data Visualization with R Andrew Young School of Policy Studies May 2020 1 / 28 Plan for today Making interactive graphics Sharing content 2 / 28 Making interactive graphics 3 / 28 Three general


slide-1
SLIDE 1

Interactivity

Session 10 PMAP 8921: Data Visualization with R Andrew Young School of Policy Studies May 2020

1 / 28

slide-2
SLIDE 2

Plan for today

Making interactive graphics Sharing content

2 / 28

slide-3
SLIDE 3

Making interactive graphics

3 / 28

slide-4
SLIDE 4

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

Complete interactive apps with Shiny

Super complicated!

4 / 28

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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)

Plotly

300 1000 3000 10000 30000 40 50 60 70 80 Africa Americas Asia Europe Oceania

gdpPercap lifeExp continent 6 / 28

slide-7
SLIDE 7

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

Plotly tooltips

300 1000 3000 10000 30000 40 50 60 70 80 Africa Americas Asia Europe Oceania

gdpPercap lifeExp continent 7 / 28

slide-8
SLIDE 8

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

Works with most geoms!

10 20 30 40 10 20 30

hwy count 8 / 28

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

11 / 28

slide-12
SLIDE 12

Dashboards with flexdashboard

Use basic R Markdown to build a dashboard!

12 / 28

slide-13
SLIDE 13

Dashboards with flexdashboard

Make any kind of block arrangement

13 / 28

slide-14
SLIDE 14

Dashboards with flexdashboard

Add other elements like text and gauges

14 / 28

slide-15
SLIDE 15

Example dashboards

ggplot2 geoms

15 / 28

slide-16
SLIDE 16

Example dashboards

NBA scoring

16 / 28

slide-17
SLIDE 17

Example dashboards

Utah's COVID-19 dashboard

17 / 28

slide-18
SLIDE 18

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

slide-19
SLIDE 19

Three general methods

Single plots with plotly

Easy!

Dashboards with flexdashboard

Slightly more complicated

Complete interactive apps with Shiny

Super complicated!

19 / 28

slide-20
SLIDE 20

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

slide-21
SLIDE 21

Lots of resources to help start

RStudio has a whole website for helping you get started

Getting started with Shiny

21 / 28

slide-22
SLIDE 22

Really neat examples!

iSEE (interactive SummarizedExperiment Explorer)

22 / 28

slide-23
SLIDE 23

Really neat examples!

COVID-19 tracker

23 / 28

slide-24
SLIDE 24

Really neat examples!

Living in the LEGO world

24 / 28

slide-25
SLIDE 25

flexdashboard + Shiny

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

I have done this

25 / 28

slide-26
SLIDE 26

Sharing content

26 / 28

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Places to put HTML documents

RPubs for knitted HTML documents

Built in to RStudio; works with ggplotly!

RPubs or shinyapps.io for flexdashboards Your own web server for anything, if you have one

28 / 28