Data

# A tibble: 6 x 8
  species island culmen_length culmen_depth flipper_length body_mass sex    year
  <fct>   <fct>          <dbl>        <dbl>          <int>     <int> <fct> <int>
1 Adelie  Torge…          39.1         18.7            181      3750 male   2007
2 Adelie  Torge…          39.5         17.4            186      3800 fema…  2007
3 Adelie  Torge…          40.3         18              195      3250 fema…  2007
4 Adelie  Torge…          36.7         19.3            193      3450 fema…  2007
5 Adelie  Torge…          39.3         20.6            190      3650 male   2007
6 Adelie  Torge…          38.9         17.8            181      3625 fema…  2007
      species          island    culmen_length    culmen_depth   flipper_length
 Adelie   :146   Biscoe   :163   Min.   :32.10   Min.   :13.10   Min.   :172   
 Chinstrap: 68   Dream    :123   1st Qu.:39.50   1st Qu.:15.60   1st Qu.:190   
 Gentoo   :119   Torgersen: 47   Median :44.50   Median :17.30   Median :197   
                                 Mean   :43.99   Mean   :17.16   Mean   :201   
                                 3rd Qu.:48.60   3rd Qu.:18.70   3rd Qu.:213   
                                 Max.   :59.60   Max.   :21.50   Max.   :231   
   body_mass        sex           year     
 Min.   :2700   female:165   Min.   :2007  
 1st Qu.:3550   male  :168   1st Qu.:2007  
 Median :4050                Median :2008  
 Mean   :4207                Mean   :2008  
 3rd Qu.:4775                3rd Qu.:2009  
 Max.   :6300                Max.   :2009  

Static graphic

library(tidyverse)
g <- ggplot(penguins, aes(x=flipper_length, y=body_mass, col=species))+
          geom_point() + theme_bw(base_size=16) 
g

Interactive graphics

  • allows the use to interact with the graphical information presented on the display.

  • Cross filtering

    • Zoom by selecting an area of interest

    • Hover the line to get exact information.

Creating Interactive plots

Method 1: ggplotly

ggplotly() converts your static plots to an interactive web-based version.

Example 1

library(plotly)
ggplotly(g)

Example 2

p <- ggplot(penguins, aes(x=body_mass, color=sex)) + 
  geom_freqpoly(stat = "density") + 
  facet_wrap(~species)
ggplotly(p)

Example 3

library(gapminder)
head(gapminder, 25)
# A tibble: 25 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.
 7 Afghanistan Asia       1982    39.9 12881816      978.
 8 Afghanistan Asia       1987    40.8 13867957      852.
 9 Afghanistan Asia       1992    41.7 16317921      649.
10 Afghanistan Asia       1997    41.8 22227415      635.
# … with 15 more rows
p1 <- ggplot(gapminder, aes(gdpPercap, pop, color = continent, frame = year)) +
  geom_point(aes(size = pop, ids = country)) + 
   geom_smooth(method = "lm", se=FALSE) +
  scale_x_log10() +
  theme_bw()
Warning: Ignoring unknown aesthetics: ids

ggplotly(p1)
`geom_smooth()` using formula 'y ~ x'

Example 4

p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country, frame=year)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) 

ggplotly(p2)

Method 2: plotly

  • powered by the JavaScript library plotly.js

  • Mapping variables: Instead of aes use ~

  • plotly finds a suitable geometric representation for us.

  • Users can also define geometries.

  • Functions to encode data

    • color: map each level to a different colour
    • colors: specify a range of colours
    • stroke
    • span
    • symbol
    • linetype

Example 1 - 1

plot_ly(penguins, x = ~species)
plot_ly(penguins, x = ~species, y= ~island)
plot_ly(
  penguins, 
  x = ~species, 
  color = "green"
) # Doesn't give green bars
plot_ly( penguins, x = ~species, color = I("green"), stroke = I("black"), span = I(2) )

Example 2 - 1

library(magrittr)
plot_ly(penguins, x = ~flipper_length, y = ~body_mass, color=~species)

Example 2 - 2

There are a family of add_* functions.

library(magrittr)
plot_ly(penguins, x = ~flipper_length, y = ~body_mass) %>%  add_markers(color=~species)

Example 3

library(magrittr)
plot_ly(penguins, x = ~flipper_length, y = ~body_mass, z = ~culmen_depth, color=~species)
library(magrittr)
plot_ly(penguins, x = ~flipper_length, y = ~body_mass, z = ~culmen_depth) %>% add_markers(color=~species)
library(magrittr)
df <- data.frame(x=1:100, y=rnorm(100))
plot_ly(df, x = ~x, y = ~y) 
library(magrittr)
df <- data.frame(x=1:100, y=rnorm(100))
plot_ly(df, x = ~x, y = ~y)  %>%
  add_lines(color = I("black"))
plot_ly(df, x = ~x, y = ~y)  %>%
   add_trace(y = ~y,  mode = 'markers')
plot_ly(df, x = ~x, y = ~y)  %>%
   add_trace(y = ~y,  mode = 'line')
plot_ly(df, x = ~x, y = ~y)  %>%
   add_trace(y = ~y,   mode = 'lines+markers')

Modify graphs: add_*

add_histogram

penguins %>% plot_ly() %>% add_histogram(x=~body_mass)

add_bars (requires pre calculated counts)

penguins %>% dplyr::count(species) %>%
  plot_ly() %>% add_bars(x=~species, y=~n)
library(dplyr)

penguins %>%
  plot_ly(x = ~species) %>% 
  add_histogram() %>%
  group_by(species) %>%
  summarise(n = n()) %>%
  add_text(
    text = ~scales::comma(n), y = ~n, 
    textposition = "top middle", 
    cliponaxis = FALSE
  )

A Grammar of Animated Graphics

gganimate is an extension of the ggplot2 package for creating animated ggplots.

library(ggplot2)
library(gganimate)

g <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) 
g

ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')