--- title: "Dynamic Visualization" --- ```{r setup, echo=F, results='hide'} knitr::opts_knit$set(root.dir = NULL) ``` # HTML Visualization ## DataTables [DataTables](http://rstudio.github.io/DT/) display R data frames as interactive HTML tables (with filtering, pagination, sorting, and search). This is a great way to make your raw data browsable without using too much space. ```{r} library(widgetframe) library(DT) datatable(iris, options = list(pageLength = 5)) ``` ## rbokeh Interface to the [Bokeh](http://hafen.github.io/rbokeh) library for making interactive graphics. ```{r, warning=F, message=F} library(rbokeh) figure(width = 400, height=400) %>% ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species, glyph = Species, hover = list(Sepal.Length, Sepal.Width)) ``` ## Leaflet [Leaflet](http://rstudio.github.io/leaflet/) is a really powerful JavaScript library for creating dynamic maps that support panning and zooming along with various annotations like markers, polygons, and popups. ```{r, warning=F, message=F} library(leaflet) library(ggmap) geocode("Buffalo, NY") m <- leaflet() %>% setView(lng = -78.87837, lat = 42.88645, zoom = 12) %>% addTiles() frameWidget(m,height =500) ``` ## dygraphs Provides rich facilities for charting time-series data in R, including highly configurable series- and axis-display and interactive features like zoom/pan and series/point highlighting. ```{r, warning=F} library(dygraphs) dygraph(nhtemp, main = "New Haven Temperatures",height = 100) %>% dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))%>% frameWidget(height =500) ``` ## rthreejs Create interactive 3D scatter plots, network plots, and globes using the ['three.js' visualization library](https://threejs.org). ```{r, message=F, results=F} #devtools::install_github("bwlewis/rthreejs") library(threejs) z <- seq(-10, 10, 0.1) x <- cos(z) y <- sin(z) scatterplot3js(x, y, z, color=rainbow(length(z))) ``` ## networkD3 Creates 'D3' 'JavaScript' network, tree, dendrogram, and Sankey graphs from 'R'. ```{r, message=F, results=F} library(igraph) library(networkD3) ``` ## Load example network This loads an example social network of friendships between 34 members of a karate club at a US university in the 1970s. See W. W. Zachary, An information flow model for conflict and fission in small groups, Journal of Anthropological Research 33, 452-473 (1977). ```{r} karate <- make_graph("Zachary") wc <- cluster_walktrap(karate) members <- membership(wc) # Convert to object suitable for networkD3 karate_d3 <- igraph_to_networkD3(karate, group = members) ``` ## Force directed network plot ```{r} forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes, Source = 'source', Target = 'target', NodeID = 'name', Group = 'group')%>% frameWidget(height =500) ``` ## Sankey Network graph Sankey diagrams are flow diagrams in which the width of the arrows is shown proportionally to the flow quantity. ```{r} # Load energy projection data library(jsonlite) URL <- paste0( "https://cdn.rawgit.com/christophergandrud/networkD3/", "master/JSONdata/energy.json") Energy <- fromJSON(URL) ``` --- ```{r} sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", units = "TWh", fontSize = 12, nodeWidth = 30)%>% frameWidget(height =500) ``` ## Radial Network ```{r} URL <- paste0( "https://cdn.rawgit.com/christophergandrud/networkD3/", "master/JSONdata//flare.json") ## Convert to list format Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE) # Use subset of data for more readable diagram Flare$children = Flare$children[1:3] ``` --- ```{r} radialNetwork(List = Flare, fontSize = 10, opacity = 0.9, height = 400, width=400)%>% frameWidget(height =500) ``` ## Diagonal Network ```{r} diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9, height = 400, width=400)%>% frameWidget(height =500) ``` ## rglwidget RGL provides 3D interactive graphics, including functions modelled on base graphics (`plot3d()`, etc.) as well as functions for constructing representations of geometric objects (`cube3d()`, etc.). You may need to install [XQuartz](https://www.xquartz.org/). ```{r, message=F} library(rgl) library(rglwidget) library(htmltools) theta <- seq(0, 6*pi, len=100) xyz <- cbind(sin(theta), cos(theta), theta) lineid <- plot3d(xyz, type="l", alpha = 1:0, lwd = 5, col = "blue")["data"] ``` ## Animate an interactive 3D process ```{r} browsable(tagList( rglwidget(elementId = "example", width = 500, height = 400, controllers = "player"), playwidget("example", ageControl(births = theta, ages = c(0, 0, 1), objids = lineid, alpha = c(0, 1, 0)), start = 1, stop = 6*pi, step = 0.1, rate = 6,elementId = "player") ))%>%save_html(file="rgl.html") ``` ---