This is the script for the htmlwidgets for R demo at the R-Ladies Coding Club’s May Event. Published doc available on RPubs: http://rpubs.com/crt34/widgetdemo
– A. Time-Series Plot
– B. Heatmap
– C. 3D Scatterplot
– D. 3D Globe
A growing number of htmlwidget R libraries have been developed from popular JavaScript charting libraries, such as D3.js, plotly.js, Leaflet.js etc.
This htmlwidgets framework/interface therefore allows R users to generate interactive JavaScript data visualisations using R code, through the normal operation of loading relevant libraries and calling appropriate functions.
These interactive plots can be displayed in RStudio, embedded in R markdown documents and Shiny web apps, and shared in standalone html files over email, dropbox etc.
– Data Storytelling: presenting & communicating findings
– Exploratory Data Analysis (EDA)
install.packages("dygraphs")
install.packages("xts")
install.packages("d3heatmaps")
install.packages("threejs")
Produce an interactive time-series visualisation using the dygraphs R library. Objects for plotting must be in xts format (extensible time series).
Time-series data = Australian wine sales in no. of bottles, by wine makers, between Jan 1980 - Aug 1994.
Using the stl function to perform the seasonal trend decomposition, which extracts the seasonal component, trend, component and remainder from the original data.
library(dygraphs)
library(xts)
In this instance the Australian Wine dataset wineind being used is from the forecast package so we load this first, then call the data.
(Original source: http://data.is/TSDLdemo)
library(forecast)
data("wineind")
?wineind
class(wineind)
wineind
plot(wineind) #base R plot
Apply stl algorithm, which divides the time series into 3 components, trend, seasonality & remainder, using Loess (a method for estimating nonlinear relationships).
stl documentation: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/stl.html
stl_wine <- stl(wineind, s.window = "periodic") #s.window controls variation of seasonal component
stl_wine
plot(stl_wine) #base R plot
First, convert each decomposed component into a separate ts (time series) object, and then into xts (extensible time series) object:
#(1980, 1) is start year & unit of observations, 12 is the no. of observations per period i.e. monthly
random_stl <- ts(stl_wine$time.series[,3], start = c(1980, 1), frequency=12)
random_stl.xts <- as.xts(random_stl)
seasonal_stl <- ts(stl_wine$time.series[,1], start = c(1980, 1), frequency=12)
seasonal_stl.xts <- as.xts(seasonal_stl)
trend_stl <- ts(stl_wine$time.series[,2], start = c(1980, 1), frequency=12)
trend_stl.xts <- as.xts(trend_stl)
Secondly, column bind the 3 xts components together to create a single xts object for plotting:
wine.plot <- cbind(random_stl.xts, seasonal_stl.xts, trend_stl.xts)
#re-name columns
colnames(wine.plot) <- c("remainder", "seasonal", "trend")
Basic:
dygraph(wine.plot) %>%
dyOptions(stackedGraph = TRUE)
Add Title & Range Selector & change colours:
dygraph(wine.plot, main = "Australian Wine Sales") %>%
dyOptions(stackedGraph = TRUE, colors=RColorBrewer::brewer.pal(3, "Set1"))%>%
dyRangeSelector()
Produce a D3 heatmap using the d3heatmap R library.
library(d3heatmap)
Data created from personal burger blog, in wide format necessary to use this library. (https://burgerite.blogspot.co.uk)
Data available here: https://github.com/RLadiesCodingLondon/MayEvent-Chiin-htmlwidgets
burger <- read.csv("burger.csv", header = TRUE, row.names = 1)
5 attributes, 25 burgers reviewed.
View(burger)
Blue = higher score, Red = lower score
d3heatmap(burger)
Produce a 3D scatterplot using the threejs R library using same burger data.
library(threejs)
scatterplot3js(burger$proportions, burger$ingredients, burger$patty, flip.y=FALSE)
#flip.y to control direction of y-axis
label <- c("proportions", "patty", "ingredients") #define axis labels
scatterplot3js(burger$proportions, burger$ingredients, burger$patty,
axisLabels = label,
labels=row.names(burger),
flip.y = FALSE,
color=rainbow(length(burger$value)),
stroke = NULL,
size=burger$value/5,
renderer = "canvas")
Produce a 3D globe, again using the threejs R library.
Geo-location data from Google Analytics of burger blog traffic.
Data available here: https://github.com/RLadiesCodingLondon/MayEvent-Chiin-htmlwidgets
views <- read.csv("globejs.csv", header = TRUE, row.names = 1)
169 cities with cumulative blog page views >= 2 since Sep ’15.
View(views)
globejs(lat = views$Lat, long = views$Long, value = views$PageViews)
traffic <- 10000*views$PageViews/max(views$PageViews)
globejs(lat = views$Lat, long = views$Long, value = traffic,
atmosphere = TRUE,
pointsize = 0.5)