plotly Exercises in R: 20 Practice Problems

Twenty practice problems on plotly in R: interactive plots, ggplotly, layouts, hover info, animations, subplots. Hidden solutions.

RRun this once before any exercise
library(plotly) library(ggplot2) library(dplyr) library(htmlwidgets)

  

Exercise 1: Scatter

Difficulty: Beginner.

Show solution
RInteractive R
plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", mode = "markers")

  

Exercise 2: Lines

Difficulty: Beginner.

Show solution
RInteractive R
plot_ly(economics, x = ~date, y = ~unemploy, type = "scatter", mode = "lines")

  

Exercise 3: Color by group

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, type = "scatter", mode = "markers")

  

Exercise 4: Bar chart

Difficulty: Beginner.

Show solution
RInteractive R
counts <- diamonds |> count(cut) plot_ly(counts, x = ~cut, y = ~n, type = "bar")

  

Exercise 5: Histogram

Difficulty: Beginner.

Show solution
RInteractive R
plot_ly(diamonds, x = ~price, type = "histogram")

  

Exercise 6: Box plot

Difficulty: Beginner.

Show solution
RInteractive R
plot_ly(iris, y = ~Sepal.Length, color = ~Species, type = "box")

  

Exercise 7: Custom hover text

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(mtcars, x = ~wt, y = ~mpg, text = ~rownames(mtcars), hoverinfo = "text+x+y")

  

Exercise 8: Layout title and axes

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", mode = "markers") |> layout(title = "Weight vs MPG", xaxis = list(title = "Weight"), yaxis = list(title = "MPG"))

  

Exercise 9: ggplotly conversion

Difficulty: Intermediate.

Show solution
RInteractive R
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() ggplotly(p)

  

Exercise 10: Subplots

Difficulty: Advanced.

Show solution
RInteractive R
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg) p2 <- plot_ly(mtcars, x = ~hp, y = ~mpg) subplot(p1, p2)

  

Exercise 11: Animation by frame

Difficulty: Advanced.

Show solution
RInteractive R
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, frame = ~Species, type = "scatter", mode = "markers")

  

Exercise 12: 3D scatter

Difficulty: Advanced.

Show solution
RInteractive R
plot_ly(mtcars, x = ~wt, y = ~mpg, z = ~hp, type = "scatter3d", mode = "markers")

  

Exercise 13: Heatmap

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(z = ~as.matrix(cor(mtcars)), type = "heatmap")

  

Exercise 14: Filled area

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(economics, x = ~date, y = ~unemploy, type = "scatter", mode = "lines", fill = "tozeroy")

  

Exercise 15: Pie

Difficulty: Beginner.

Show solution
RInteractive R
counts <- diamonds |> count(cut) plot_ly(counts, labels = ~cut, values = ~n, type = "pie")

  

Exercise 16: Log y axis

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(diamonds, x = ~carat, y = ~price, type = "scatter", mode = "markers") |> layout(yaxis = list(type = "log"))

  

Exercise 17: Range slider (time series)

Difficulty: Advanced.

Show solution
RInteractive R
plot_ly(economics, x = ~date, y = ~unemploy, type = "scatter", mode = "lines") |> layout(xaxis = list(rangeslider = list()))

  

Exercise 18: Custom color palette

Difficulty: Intermediate.

Show solution
RInteractive R
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = c("red","blue","green"))

  

Exercise 19: Save to HTML

Difficulty: Intermediate.

Show solution
RInteractive R
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) htmlwidgets::saveWidget(p, "plot.html")

  

Exercise 20: Combine with crosstalk

Difficulty: Advanced.

Show solution
RInteractive R
library(crosstalk) shared <- SharedData$new(mtcars) plot_ly(shared, x = ~wt, y = ~mpg) |> highlight("plotly_selected")

  

What to do next

  • ggplot2-Exercises (shipped), static viz.
  • Shiny-Exercises (shipped), embed plotly inside apps.