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(DT)

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

The DataTables (DT) package provides a quick way to make data tables interactive. Simply put, it enables tables to be searchable, sortable, and pageable automatically.

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](https://www.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

Plot.ly specializes in online dynamic data visualizations and, in particular, the ability to translate code to generate data graphics between R, Python, and other data software tools. This project is based on the plotly.js JavaScript library, which is available under an open-source license. The functionality of Plot.ly can be accessed in R through the plotly package.

What makes plotly especially attractive is that it can convert any ggplot2 object into a plotly object using the ggplotly() function.

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.3     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ 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] "/Users/wenling/Desktop/Stat 651"
write_csv(Beatles, file.path("Beatles.csv"))
ggplotly(beatles_plot) # shows the exact values by mousing-over the lines.

Dygraphs

The dygraphs package generates interactive time series plots with the ability to brush over time intervals and zoom in and out.

library(dygraphs)

Beatles %>% 
  filter(sex == "M") %>% 
  select(year, name, prop) %>%
  pivot_wider(names_from = name, values_from = prop) %>%
  dygraph(main = "Popularity of Beatles names over time") %>% 
  dyRangeSelector(dateWindow = c("1940", "1980"))
# pivot_wider() function from the tidyr package in R can be used to pivot a data frame from a long format to a wide format. where: names_from: The column whose values will be used as column names. values_from: The column whose values will be used as cell values.

Streamgraphs

A streamgraph, or stream graph, is a type of stacked area graph which is displaced around a central axis, resulting in a flowing, organic shape.

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.

A streamgraph is a particular type of time series plot that uses area as a visual cue for quantity. Streamgraphs allow you to compare the values of several time series at once. The streamgraph htmlwidget provides access to the streamgraphs.js D3 library.

# library(devtools)

# install_github("hrbrmstr/streamgraph")

library(streamgraph)
library(RColorBrewer)

Beatles %>% streamgraph(key = "name", value = "n", date = "year") %>%
  sg_fill_brewer(palette = "Accent")  # RColorBrewer, palette= is optional 
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)  # for str_detect() function

babynames %>%
  filter(str_detect(name, "^Kr")) %>% # names started with Kr
  group_by(year, name) %>%
  tally(wt=n) %>%        # tally() is pretty similar to summarise()
  streamgraph("name", "n", "year")
babynames %>%
  filter(str_detect(name, "^I")) %>% # names started with I
  group_by(year, name) %>%
  tally(wt=n) %>%
  streamgraph("name", "n", "year", offset="zero", interpolate="linear") %>%
  sg_legend(show=TRUE, label="I- names: ")
# offset="zero":  the baseline for the streamgraph will start at zero.
# interpolate = "linear": specifies the interpolation method used to create the smooth transitions between streamgraph segments.
# sg_legend(): configures the legend for the streamgraph. It takes the following arguments:
# show = TRUE: indicates that the legend should be displayed.
# label = "I- names: ": sets the label for the legend, specifying all names starting with "I-"

Animations: gganimate package

The gganimate package provides a simple way to create animations (i.e., GIFs) from ggplot2 data graphics

library(gganimate)
library(transformr)
beatles_animation <- beatles_plot + 
  transition_states(
    name,
    transition_length = 2,
    state_length = 1
  ) +
  enter_grow() + 
  exit_shrink()

# this line specifies how the animation should transition between different states. It seems to transition based on the variable name, with a transition length of 2 and s state length of 1. This means the animation will transition between different states with a duration of 2 units and stay in each state for 1 unit of time.

animate(beatles_animation, height = 400, width = 800)

Flexdashboard

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

The flexdashboard package provides a straightforward way to create and publish data visualizations as a dashboard. Dashboards are a common way that data scientists make data available to managers and others to make decisions. They will often include a mix of graphical and textual displays that can be targeted to their needs.

library(flexdashboard)
library(palmerpenguins)
library(tidyverse)

### Chart B
DT::datatable(penguins)
### Chart A
ggplot(penguins,
  aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point()
Warning: Removed 2 rows containing missing values (`geom_point()`).

### Chart C

roundval <- 2
cleanmean <- function(x, roundval = 2, na.rm = TRUE) {
  return(round(mean(x, na.rm = na.rm), digits = roundval))
}
summarystat <- penguins %>%
  group_by(species) %>%
  summarize(
    `Average bill length (mm)` = cleanmean(bill_length_mm),
    `Average bill depth (mm)` = cleanmean(bill_depth_mm)
  )
knitr::kable(summarystat)
species Average bill length (mm) Average bill depth (mm)
Adelie 38.79 18.35
Chinstrap 48.83 18.42
Gentoo 47.50 14.98

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()