Interactive scatterplot

Convert one of the scatterplots you generated previously using the following pattern:

p <-
  data %>% 
  ___ %>%
  ggplot(aes(___)) +
    geom_point()
  
plotly::ggplotly(p)

► Solution:

p <-
  mpg %>% 
  ggplot(aes(x = displ, y = hwy, color = drv)) +
    geom_point()

plotly::ggplotly(p)
p2 <-
  mpg %>% 
  ggplot(aes(x = displ, y = hwy, color = drv)) +
    geom_jitter() +
    geom_smooth()

plotly::ggplotly(p2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

More expressive tooltip

Experiment with the plotly controls on the top right.

Add extra aesthetics to the scatterplot. Hover with the mouse over the points:

p <-
  data %>% 
  ___ %>%
  ggplot(aes(___, extra1 = manufacturer, extra2 = model)) +
    geom_jitter()
  
plotly::ggplotly(p)

► Solution:

p <-
  mpg %>% 
  ggplot(aes(x = displ, y = hwy, extra1 = manufacturer, extra2 = model)) +
    geom_jitter()

plotly::ggplotly(p)

Barplot

Convert a bar plot or a histogram to plotly. How does this affect the tooltip?

► Solution:

p <-
  mpg %>% 
  ggplot(aes(x = drv)) +
    geom_bar()

plotly::ggplotly(p)

Crosstalk

See https://plotly-book.cpsievert.me/linking-views-without-shiny.html for examples of linking two plots, or linking plots with other widgets. This requires the crosstalk package:

install.packages("crosstalk")

Copyright © 2019 Kirill Müller. Licensed under CC BY-NC 4.0.