library(shiny)
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, plotly, stats
## lag():    dplyr, stats
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
load("~/Dropbox/Documents/SMU/CSC 463/cdc.Rdata")
load("~/Dropbox/Documents/SMU/CSC 463/Shiny/olywthr.rdata")

Using plot_ly

This function produces plotly objects by itself. Read the documentation in the plotly package.

str(economics)
## Classes 'tbl_df', 'tbl' and 'data.frame':    574 obs. of  6 variables:
##  $ date    : Date, format:
## Warning in format.POSIXlt(as.POSIXlt(x), ...): unknown timezone 'default/
## America/Los_Angeles'
## "1967-07-01" "1967-08-01" ...
##  $ pce     : num  507 510 516 513 518 ...
##  $ pop     : int  198712 198911 199113 199311 199498 199657 199808 199920 200056 200208 ...
##  $ psavert : num  12.5 12.5 11.7 12.5 12.5 12.1 11.7 12.2 11.6 12.2 ...
##  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: int  2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...
plot_ly(economics, x = ~pop)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#histogram
# plot_ly() tries to create a sensible plot based on the information you 
# give it. If you don't provide a trace type, plot_ly() will infer one.
plot_ly(economics, x = ~pop)
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#histogram
plot_ly(economics, x = ~date, y = ~pop)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

You can control the appearance by using the add_() functions, much like geom_() in ggplot2()

plot_ly(economics, x = ~date, y = ~pop) %>% add_markers()
# Note that there are actually points here.  You can see by zooming in.

You can ask for a line graph by adding lines instead of markers.

plot_ly(economics, x = ~date, y = ~pop) %>% add_lines()
# Verify that this is a line by zooming in.

Note that instead of piping you can create a plotly object and use it as the first argument to produce many different graphs.

p <- plot_ly(iris, x = ~Sepal.Width, y = ~Sepal.Length) 
add_markers(p, color = ~Petal.Length, size = ~Petal.Length)
add_markers(p, color = ~Species)
add_markers(p, color = ~Species, colors = "Set1")
add_markers(p, symbol = ~Species)
add_paths(p, linetype = ~Species)

The last one, with lines, makes no sense.

Shiny + Plotly Demo

I set eval to false so this code will not be run when knitting.

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

server <- 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(ui, server)

Animation

Look at Animation in the package documentation.

# Copy to the console and zoom for a clear view. Specifying the frame argument triggers an animation.
plot_ly(cdc,x=~height,y=~weight,frame=~age)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

An Example using olywthr

olywthr %>% 
  group_by(mo,dy) %>% 
  filter(!(mo == 2 & dy == 29)) %>% 
  summarise(prain = mean(PRCP > 0)) %>% 
  ungroup() %>% 
  mutate(date = make_date(2018,mo,dy)) -> rainprob

# Create the probability of rain graph
prg =   rainprob %>% ggplot(aes(x=date,y=prain)) + 
    geom_point()

# Display the ggplot result
prg

# Run it through ggplotly to make it interactive
ggplotly(prg)


# Let's try animation
rainprob $nday = 1:365

# Copy to console 
plot_ly(rainprob,x=~nday,y=~prain,frame=~nday)