Interactive Plots in R

Anthony Pagan

December 2, 2018

Introduction - Buiding Interactive Plots in R

Plotly Overview

Shiny Overview

Using Plotly

Basic Example 1

library(plotly)
library(ggplot2)
plot_ly(economics, x = ~pop)

Basic Example 2

#options(browser = 'FALSE')
t<-plot_ly(midwest, x = ~percollege, color = ~state, 
type = "box",width = 800, height = 300)%>%
layout(margin = list(l = 100))
t
rangeslider(t)

Using Plotly with GGPlotly: GGPLOT

p <- ggplot(txhousing, aes(date, median,
    width = 800, height = 300)) +
    geom_line(aes(group=city), alpha = 0.2)
p

Using Plotly with GGPlotly

#options(browser = 'FALSE')
subplot(
    p, ggplotly(p, tooltip = "city",
                width = 800, height = 300),
    ggplot(txhousing, aes(date, median)) + geom_bin2d(),
    ggplot(txhousing, aes(date, median)) + geom_hex(),
    nrows =2, shareX= TRUE, shareY = TRUE,
    titleY = FALSE, titleX = FALSE
    )

Statistical Example

library(plotly)
library(GGally)
pm<- ggpairs(iris)
ggplotly(pm,width = 800, height = 600)

Plotly online example

Sys.setenv("plotly_username"="apag101")
Sys.setenv("plotly_api_key"="OyQslu5MKVQcEjx7aVJV")
api_create(t, filename = c("test"))

Using Plotly Web Figures

#Download from current account
api_download_plot("8","apag101")
#Download from https://plot.ly/~cpsievert/559/_1-vs-2/#/
api_download_plot("559", "cpsievert")

Using Shiny

App R Shiny Example

library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
   # Application title
   titlePanel("Old Faithful Geyser Data"),
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),
      
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

Shiny Online Example

#install.packages('rsconnect')

rsconnect::setAccountInfo(name='apag101', token='98AFD680E6AB821CF1F430EF2A6DABFC', secret='rgMjLtYC8JcXgTFRb5TWOSzKlLXk4W4Nkp4fr1eU')

library(rsconnect)
rsconnect::deployApp('C:/Users/apagan/Documents/test2')

Shiny App With Plotly

ui2 <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("event")
)

server2 <- function(input, output) {

  # renderPlotly() also understands ggplot2 objects!
  output$plot <- renderPlotly({
    plot_ly(mtcars, x = ~mpg, y = ~wt)
  })

  output$event <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover on a point!" else d
  })
}

shinyApp(ui2, server2)

Recap

QUESTIONS?