Plotly Example Cookbook

Plotly is an open source, interactive, data visualization tool that interfaces R, Matlab, and Python with web based JavaScript graphics. Built for collaboration, plotly offers online hosting, technical support, and many visualization tools with both the free and paid subscription plans. In this post, I dive in to up setting a plotly environment and create a variety of visualizations to showcase plotly’s abilities. The visualization examples are inspired by, and in some cases directly copied from the eBook Plotly for R by Carson Sievert. Carson is an interactive data visualization powerhouse, the mastermind behind plotly, and one of my data science heroes.

Plotly supports both online and offline functionality. If wish to forgo the tools and collaborative web support provided at https://plot.ly/feed/, simply install the package by following step one of the setup guide below. Plotly is still extremely useful without the online platform, this R Markdown document has been created entirely offline in RStudio. If you want access to the additional web features, follow the entire setup guide.


Setup

1. Install and load plotly package in R

install.packages('plotly')
library('plotly')

2. Create an online account at Plot.ly - https://plot.ly

3. Find your credentials on Plot.ly (Settings > API Keys > Regenerate Key)

4. Set your credentials in R

Sys.setenv("plotly_username"="username-goes-here")
Sys.setenv("plotly_api_key"="api-key-goes-here")

5. Create a plotly object to test

p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")

6. Push test object to Plot.ly servers

api_create(p, filename = "midwest-boxplots")


It’s that easy! Whenever you want to push an object to Plot.ly use the api_create() function. I highly encourage you at least take a peek at the online platform, it’s fun and easy to use. Here are some of my favorite examples of Plotly in R.


2D Scatterplot

subplot(
  plot_ly(mpg, x = ~cty, y = ~hwy, showlegend=FALSE) %>%
    add_markers(alpha = 0.6,
                size = ~cyl,
                sizes = c(30,50),
                color = ~cyl), 
  plot_ly(mpg, x = ~cty, y = ~hwy) %>%
    add_markers(alpha = 0.6,
                symbol = ~factor(cyl),
                symbols = c(0,1,5,10),
                color = ~factor(cyl)
                )
)


3D Scatterplot

col1 <- colorRamp(c("red", "blue"))

plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl,
              colors = col1
              )


Line Plot

library(dplyr)
top5 <- txhousing %>%
  group_by(city) %>%
  summarise(m = mean(sales, na.rm = TRUE)) %>%
  arrange(desc(m)) %>%
  top_n(5)
line_ex <- semi_join(txhousing, top5, by = "city")
col2 <- "Pastel2"

plot_ly(line_ex, x = ~date, y = ~median) %>%
  add_lines(color = ~city,
            colors = col2,
            linetype = ~city
            )


Hex Plot

p <- ggplot(txhousing, aes(date, median)) +
  geom_line(aes(group = city), alpha = 0.2)

subplot(p, 
  ggplot(txhousing, aes(date, median)) + 
    geom_hex(), 
    shareX = TRUE,
    shareY = TRUE
)


Density Plot

kerns <- c("gaussian", "epanechnikov", "rectangular", 
          "triangular", "biweight", "cosine", "optcosine")

p <- plot_ly()

for (k in kerns) {
  d <- density(txhousing$median, kernel = k, na.rm = TRUE)
  p <- add_lines(p, x = d$x, y = d$y, name = k)
}

layout(p, xaxis = list(title = "Median monthly price"))


Histogram

p1 <- plot_ly(diamonds, x = ~price) %>% 
  add_histogram(name = "plotly.js")

price_hist <- function(method = "FD") {
  h <- hist(diamonds$price, breaks = method, plot = FALSE)
  plot_ly(x = h$mids, y = h$counts) %>% 
    add_bars(name = method)
}

subplot(
  p1, 
  price_hist(),
  price_hist("Sturges"),
  price_hist("Scott"),
  nrows = 4, 
  shareX = TRUE
)


Bar Chart

p1 <- plot_ly(diamonds, x = ~cut) %>% 
  add_histogram()

p2 <- diamonds %>%
  dplyr::count(cut) %>%
  plot_ly(x = ~cut, y = ~n, showlegend=FALSE) %>% 
  add_bars()

subplot(p1, p2)