Last updated: 2018-03-29

Code version: f93372a

I came across one website introducing plotly R package, and was addicted for a few days, just share some of these amazing plots with you. The main scheme is introduce three basic examples, followed by scatter plot, line plot and ribbons.I will conclude this section with two great animation examples.

##install.packages("plotly")
##packageVersion("plotly")
library(plotly)
library(ggplot2)

Basic example 1

Interface modified by layout() function:“click + drag”event, rangeslider() will add a slider under this figure.

library(forecast)
p <- ggplot(fortify(gold), aes(x, y)) + geom_line()
gg <- ggplotly(p)
layout(gg, dragmode = "pan")
##rangeslider(gg)

Basic example 2

Look at and learn the basic structre , firstly use ggplotly(), then %>% link several matrics.

p <- ggplot(MASS::geyser, aes(x = waiting, y = duration)) +
  geom_density2d()
ggplotly(p, originalData = FALSE) %>% 
  group_by(piece) %>%
  slice(which.min(y)) %>% 
  add_text(
    text = ~level, size = I(10), color = I("black"), hoverinfo = "none"
  )

Basic example 3

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() + geom_smooth()
style(p, hoverinfo = "none", traces = 2:3)

Of course, we even could add annotations in detailed positions by function add_annotations() and add_segments().

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() + geom_smooth()
p %>%
  ggplotly(layerData = 2, originalData = FALSE) %>%
  add_fun(function(p) {
    p %>% slice(which.max(se)) %>%
      add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%
      add_annotations("Maximum uncertainty", ax = 60)
  }) %>%
  add_fun(function(p) {
    p %>% slice(which.min(se)) %>%
      add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%
      add_annotations("Minimum uncertainty")
  })

## 2D and 3D Scatter plot This time we use plot_ly(), previous sections are all employed ggplotly().

##I(2)is triangle, I(3)is +
subplot(
  plot_ly(x = 1:25, y = 1:25, symbol = I(1:25), name = "pch"),
  plot_ly(mpg, x = ~cty, y = ~hwy) %>% 
    add_markers(symbol = I(1), color = I("red"),name = "red")
)
p <- plot_ly(mpg, x = ~cty, y = ~hwy, alpha = 0.5)
subplot(
  add_markers(p, color = ~cyl, showlegend = F,name = "mpg") %>% 
    colorbar(title = "Viridis")
)

Last ones are two 3D examples, you are free to rotate the figures.

plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl)
x <- seq_len(nrow(volcano)) + 100
y <- seq_len(ncol(volcano)) + 500
plot_ly() %>% add_surface(x = ~x, y = ~y, z = ~volcano)

A powerful functiom to visualize data iris:

pm <- GGally::ggpairs(iris)
ggplotly(pm)

Line plots

Lower image are the same data with upper image, just to compare the effects of add_lines(p, color = ~city) and add_lines(p, linetype = ~city).

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

p <- semi_join(txhousing, top5, by = "city") %>%
  plot_ly(x = ~date, y = ~median)
subplot(
  add_lines(p, color = ~city),
  add_lines(p, linetype = ~city),
  shareX = TRUE, nrows = 2
)

Ribbons

library(broom)
m <- lm(mpg ~ wt, data = mtcars)
broom::augment(m) %>%
  plot_ly(x = ~wt, showlegend = FALSE) %>%
  add_markers(y = ~mpg, color = I("red")) %>%
  add_ribbons(ymin = ~.fitted - 1.96 * .se.fit, 
              ymax = ~.fitted + 1.96 * .se.fit, color = I("gray80")) %>%
  add_lines(y = ~.fitted, color = I("steelblue"))

Animation 1

Return to use function ggplotly()

data(gapminder, package = "gapminder")
head(gapminder)
# A tibble: 6 x 6
  country     continent  year lifeExp      pop gdpPercap
  <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
1 Afghanistan Asia       1952    28.8  8425333      779.
2 Afghanistan Asia       1957    30.3  9240934      821.
3 Afghanistan Asia       1962    32.0 10267083      853.
4 Afghanistan Asia       1967    34.0 11537966      836.
5 Afghanistan Asia       1972    36.1 13079460      740.
6 Afghanistan Asia       1977    38.4 14880372      786.
gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10()
ggplotly(gg)