Stat. 651 Interactive Data graphics

Examples of using R packages that depend on JavaScript and D3. We will be testing out a number of htmlwidgets

Datatable

Check out the DT. It makes very nice interactive tables that include the ability to search for rows of data.

library(DT)

datatable(mtcars)
library(nycflights13)

datatable(planes)

Leaflet

The leaflet package is very useful for making maps.

library(leaflet)

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m  # Print the map

Lets find our current location using latlong.net and make a map. So now

m_csueb <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-122.053765, lat=37.656057, popup="Stat. 651 on the CSU East Bay campus.")  
m_csueb  # Print the map

Plot.ly

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)

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(mdsr)
library(babynames)

Beatles <- babynames %>%
  filter(name %in% c("John", "Paul", "George", "Ringo") & sex == "M") %>%
  mutate(name = factor(name, levels = c("John", "George", "Paul", "Ringo")))

datatable(Beatles)
beatles_plot <- Beatles %>% ggplot(aes(x = year, y = n)) +
  geom_line(aes(color = name), size = 2)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
beatles_plot

getwd()
[1] "/home/esuess/Documents/Stat651/interactive"
write_csv(Beatles, file.path("Beatles.csv"))
ggplotly(beatles_plot)

Dygraphs

library(dygraphs)

Beatles %>% 
  select(year, name, prop) %>%
  pivot_wider(names_from = name, values_from = prop) %>%
  dygraph(main = "Popularity of Beatles names over time") %>% 
  dyRangeSelector(dateWindow = c("1965", "1975"))

Streamgraphs

The streamgraph package is not available on CRAN. It needs to be installed using devtools. To install uncomment the first two lines of the code chunk below. When asked to update other packages, select No.

# library(devtools)

# install_github("hrbrmstr/streamgraph")

library(streamgraph)
library(RColorBrewer)

Beatles %>% streamgraph(key = "name", value = "n", date = "year") %>%
  sg_fill_brewer("Accent")
Warning in widget_html(name, package, id = x$id, style = css(width =
validateCssUnit(sizeInfo$width), : streamgraph_html returned an object of class
`list` instead of a `shiny.tag`.
Warning: `bindFillRole()` only works on htmltools::tag() objects (e.g., div(),
p(), etc.), not objects of type 'list'.
library(stringr)
babynames %>%
  filter(str_detect(name, "^Kr")) %>%
  group_by(year, name) %>%
  tally(wt=n) %>%
  streamgraph("name", "n", "year")
Warning in widget_html(name, package, id = x$id, style = css(width =
validateCssUnit(sizeInfo$width), : streamgraph_html returned an object of class
`list` instead of a `shiny.tag`.
Warning: `bindFillRole()` only works on htmltools::tag() objects (e.g., div(),
p(), etc.), not objects of type 'list'.
babynames %>%
  filter(str_detect(name, "^I")) %>%
  group_by(year, name) %>%
  tally(wt=n) %>%
  streamgraph("name", "n", "year", offset="zero", interpolate="linear") %>%
  sg_legend(show=TRUE, label="I- names: ")
Warning in widget_html(name, package, id = x$id, style = css(width =
validateCssUnit(sizeInfo$width), : streamgraph_html returned an object of class
`list` instead of a `shiny.tag`.
Warning: `bindFillRole()` only works on htmltools::tag() objects (e.g., div(),
p(), etc.), not objects of type 'list'.

gganimate

library(gganimate)
No renderer backend detected. gganimate will default to writing frames to separate files
Consider installing:
- the `gifski` package for gif output
- the `av` package for video output
and restarting the R session
library(transformr)
beatles_animation <- beatles_plot + 
  transition_states(
    name,
    transition_length = 2,
    state_length = 1
  ) +
  enter_grow() + 
  exit_shrink()

animate(beatles_animation, height = 400, width = 800)
Warning: No renderer available. Please install the gifski, av, or magick
package to create animated output

Flexdashboard

Check out the flexdashboard website. To create a new flex dashboard we will install the package and then load an R Markdown Template.

library(flexdashboard)

Shiny documents

Check out Shiny documents Chapter in the RMarkdown book. To create a new Shiny document we will load an R Markdown Template.

Shiny App packages

  1. seasonalview, this link runs it online.

You can install the package and run it locally.

library(seasonalview)

help(seasonalview)

m <- seas(AirPassengers)
view(m)

# standalone()