R plots are cool, but interactive R plots are even cooler.

HTMLwidgets is a library you may not use directly, but it enables package creators to easily port JavaScript visualisations to R for display. Since JavaScript has arguably the best visualisation libraries out there, led by https://d3js.org/, this opens up many opportunities to link R data with JavaScript interactivity.

A few notable examples are shown below using our example data.

This exercise requires having a web_data data frame. You can either load up some sample data by completing the I/O Exercise (which is what is shown in the step-by-step instructions below), or, if you have access to a Google Analytics account, you can use your own data by following the steps on the Google Analytics API page. Or, if you have access to an Adobe Analytics account, then you can use your own data by following the Generating web_data steps on the Adobe Analytics API page.

Once you have a web_data data frame to work with, the command head(web_data) should return a table that, at least structurally, looks something like this:
date channelGrouping deviceCategory sessions pageviews entrances bounces
2016-01-01 (Other) desktop 19 23 19 15
2016-01-01 (Other) mobile 112 162 112 82
2016-01-01 (Other) tablet 24 41 24 19

We have to start with a bit of data prep:

library(tidyr)
library(dplyr)
## call the key column 'variable' and the value colum 'value' and 
## gather all variables apart from date, channelGrouping and deviceCategory
## get only desktop rows, and the date, channelGrouping and sessions columns
pivoted <- web_data %>% 
  filter(deviceCategory == "desktop") %>% 
  select(date, channelGrouping, sessions) %>%
  spread(channelGrouping, sessions)

## get rid of any NA's and replace with 0
pivoted[is.na(pivoted)] <- 0

Plots

Dygraphs

Good for time-series. Click and drag on the plot to zoom. Double-click to reset.

library(dygraphs)
library(xts)

## dygraph likes date objects
pivoted$date <- as.Date(pivoted$date)

## dygraph likes xts objects
web_data_xts <- xts(pivoted[,-1], order.by = pivoted$date)

dygraph(web_data_xts)

Plot.ly

Includes very handy ggplotly() function to turn ggplot objects into interactive versions:

library(tidyr)
library(dplyr)

## call the key column 'variable' and the value colum 'value' and 
## gather all variables apart from date, channelGrouping and deviceCategory
web_data_tidy <- web_data %>% gather(variable, value, -date, -channelGrouping, -deviceCategory)
web_data_tidy$date <- as.Date(web_data_tidy$date)

plot_data <- web_data_tidy %>% filter(variable == "sessions")

library(ggplot2)
## I don't know why, but I always call them gg
gg <- ggplot(data = plot_data, aes(x = date)) + theme_minimal()
gg <- gg + geom_area(aes(y = value, group = channelGrouping, fill = channelGrouping)) + facet_grid(deviceCategory ~ .)
## make the colours nicer
gg <- gg + scale_fill_brewer(palette = "Paired")
## add a title
gg <- gg + ggtitle("Sessions per device category")
## rename the x and y axis
gg <- gg + xlab("Date") + ylab("Sessions")
## change the legend title
gg <- gg + guides(fill = guide_legend(title = "Channel Grouping"))
## put the legend at the bottom
gg <- gg + theme(legend.position = "bottom")


library(plotly)
ggplotly(gg)

Highcharter

You need to pay to use this commercially, but it is a very nice library with lots of options and shortcuts to plot R objects. The below is a correlation table:

library(highcharter)
## make a time-series object
## can't include the date as its not numeric, so remove
cor_data <- pivoted[, -1]
## not including first column, so -1 subset
cor_table <- round(cor(cor_data),2)

hchart(cor_table)