Interactive Graphics

Data Visualization III

Agenda

Adding interactivity to ggplots with packages:

  • "plotly"
  • "ggiraph"

Installation

# run this command on R's console
# (don't include this in a qmd file)
install.packages(c("plotly", "ggiraph"))

"plotly"

About Plotly

Plotly is a software company that develops online data analytics and visualization tools, as well as scientific graphing libraries for R, Python, Matlab, etc.


In R, we have the package "plotly".


plotly comes with its own grammar of graphics syntax. Interestingly, we can use the ggplotly() function to make a ggplot graphic into a web-interactive graphic.

Palmer Penguins

library(palmerpenguins)
penguins
# A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <int>

ggplot(data = penguins,
       mapping = aes(x = bill_length_mm, 
                     y = flipper_length_mm,
                     color = species)) +
  geom_point()

gg1 <- ggplot(data = penguins,
       mapping = aes(x = bill_length_mm, 
                     y = flipper_length_mm,
                     color = species)) +
  geom_point()

ggplotly(gg1)

Be careful

Not all ggplot graphical elements translate to plotly

gg2 <- ggplot(data = penguins,
       mapping = aes(x = bill_length_mm, 
                     y = flipper_length_mm,
                     color = species)) +
  geom_point() +
  labs(title = "Scatter plot", 
       subtitle = "Palmen Penguins Data")

ggplotly(gg2)

"ggiraph"

About "ggiraph"

This package allows us to make ggplot2 graphics interactive following the same framewokr and syntax of adding layers to a graphic.


"ggiraph" provides sibling geom functions, e.g.:

  • geom_point_interactive()
  • geom_bar_interactive()
  • geom_col_interactive()
  • geom_histogram_interactive()
  • geom_density_interactive()
  • geom_boxplot_interactive()

ggplot(data = penguins,
       mapping = aes(x = bill_length_mm, 
                     y = flipper_length_mm,
                     color = species)) +
  geom_point()

gg3 <- ggplot(data = penguins,
       mapping = aes(x = bill_length_mm, 
                     y = flipper_length_mm,
                     color = species)) +
  geom_point_interactive(aes(tooltip = sex))

girafe(ggobj = gg3)

gg4 <- ggplot(
  data = penguins,
  aes(x = bill_length_mm, 
      y = flipper_length_mm,
       color = species,
      tooltip = sex)) +
  geom_point_interactive()

ggiraph::girafe(ggobj = gg4)