¡t ë%Ìë% ¡mt 뽡Ìmtbàë }really fancy (and easy !) dashboards! x . three general met hods...

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

Upload: others

Post on 01-Apr-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Interactivity

Session 10PMAP 8921: Data Visualization with R

Andrew Young School of Policy StudiesMay 2020

1 / 28

Page 2: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Plan for today

Making interactive graphics

Sharing content

2 / 28

Page 3: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Making interactive graphics

3 / 28

Page 4: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Three general methods

Single plots with plotlyEasy!

Dashboards with flexdashboardSlightly more complicated

Complete interactive apps with ShinySuper complicated!

4 / 28

Page 5: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Single plots with plotlyPlotly is special software for

creating interactive plots with JavaScript

No knowledge of JavaScript needed!

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

5 / 28

Page 6: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

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

gdpPercaplifeE

xp

continent

6 / 28

Page 7: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

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

gdpPercaplifeE

xp

continent

7 / 28

Page 8: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

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

0

10

20

30

hwyco

unt

8 / 28

Page 9: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Save as HTMLSave a self-contained HTML version of it withsaveWidget() in the htmlwidgets R package

# This is like ggsave, but for interactive HTML plotshtmlwidgets::saveWidget(interactive_plot, "fancy_plot.html")

9 / 28

Page 10: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Fully documentedThe documentation for ggplot2 + plotly is full of

examples of how to customize everything

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

10 / 28

Page 11: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Three general methods

Single plots with plotlyEasy!

Dashboards with flexdashboardSlightly more complicated

11 / 28

Page 12: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Dashboards with flexdashboardUse basic R Markdown to build a dashboard!

12 / 28

Page 13: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Dashboards with flexdashboardMake any kind of block arrangement

13 / 28

Page 14: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Dashboards with flexdashboardAdd other elements like text and gauges

14 / 28

Page 15: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Example dashboards

ggplot2 geoms15 / 28

Page 16: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Example dashboards

NBA scoring

16 / 28

Page 17: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Example dashboards

Utah's COVID-19 dashboard

17 / 28

Page 18: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Outstanding documentationThe documentation for flexdashboard is

full of examples and details of everything you can do

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

18 / 28

Page 19: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Three general methods

Single plots with plotlyEasy!

Dashboards with flexdashboardSlightly more complicated

Complete interactive apps with ShinySuper complicated!

19 / 28

Page 20: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Shiny

Shiny is a complete web applicationframework 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

Page 21: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Lots of resources to help startRStudio has a whole website for helping you get started

Getting started with Shiny

21 / 28

Page 22: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Really neat examples!

iSEE (interactive SummarizedExperiment Explorer)22 / 28

Page 23: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Really neat examples!

COVID-19 tracker23 / 28

Page 24: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Really neat examples!

Living in the LEGO world

24 / 28

Page 25: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

flexdashboard + ShinyYou can use reactive Shiny things in flexdashboards

without building a complete Shiny app!I have done this

25 / 28

Page 26: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Sharing content

26 / 28

Page 27: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

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

Page 28: ¡T ë%Ìë% ¡mT 뽡ÌmTbàë }really fancy (and easy !) dashboards! x . Three general met hods Single plots with plotly Easy ! Dashboards with flexdashboard Slightly more complicated

Places to put HTML documentsRPubs 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