plotly::ggplotly()

BSTA 526 Functions of the Week

Converting ggplot2 plots into interactive Plotly visualizations
Author

Isaac Gomez Hernandez

Published

February 26, 2026

The Porsche 914-2 shown below is one of the vehicles included in the mtcars dataset.

1971 Porsche 914

Image: 1971 Porsche 914. Bob Adams from Amanzimtoti, South Africa, CC BY-SA 2.0, via Wikimedia Commons.

0.1 Loading dataset

# Creating working version of the built-in mtcars dataset

mtcars_tbl <- mtcars %>%
  
  # Converting row names (car names) into an explicit column
  tibble::rownames_to_column(var = "car") %>%
  
  # Converting selected numeric variables into categorical factors
  # Showing categories rather than continuous measurements
  mutate(
    cyl = factor(cyl),  # Converting number of cylinders to factor
    am  = factor(am,
                 levels = c(0, 1),
                 labels = c("Automatic", "Manual"))  # Changing transmission type to labeled factor
  )

# Showing the first few rows of the cleaned dataset
head(mtcars_tbl)
                car  mpg cyl disp  hp drat    wt  qsec vs        am gear carb
1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0    Manual    4    4
2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0    Manual    4    4
3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1    Manual    4    1
4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1 Automatic    3    1
5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0 Automatic    3    2
6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1 Automatic    3    1

1 Function(s) Name(s)

The function being presented is ggplotly() from the plotly package in R.

2 What is it for?

2.1 The function has some useful interactive features such as:

  • Hovering over data points to see values in more detail
  • Being able to zoom in/out of specific areas of figures
  • Toggling features on/off using the legend
  • Saving figures as PNG files

3 Examples

3.1 Example 1

# Creating a scatterplot of fuel efficiency versus vehicle weight
carfig1 <- ggplot(mtcars_tbl,
             aes(x = wt, y = mpg, color = cyl)) +
  geom_point(size = 3) +
  theme_minimal() +
  labs(
    title = "1974 Fuel Efficiency vs Weight by Cylinders",
    x = "Weight (1000 lbs)",
    y = "Miles per Gallon",
    color = "Cylinders"
  )

# Showing the static ggplot
carfig1

# Converting the static ggplot into an interactive plot
ggplotly(carfig1)

3.2 Example 2

# Creating a boxplot comparing fuel efficiency by number of cylinders
# Adding custom hover text for each car

carfig2 <- ggplot(
  mtcars_tbl,
  aes(
    x = cyl,
    y = mpg,
    fill = cyl,
    text = paste0( # Specifying exact hover text
      "Car: ", car,
      "<br>MPG: ", mpg,
      "<br>Weight: ", wt,
      "<br>Transmission: ", am
    ) )
) +
  geom_boxplot() +  
 geom_jitter(width = 0.15, alpha = 0.7, shape = 21, size = 3, stroke = 0.3) +  
  # Showing individual observations
  theme_minimal() +
  labs(
    title = "1974 Fuel Efficiency by Number of Cylinders",
    x = "Number of Cylinders",
    y = "Miles per Gallon",
    fill = "Cylinders")

# Showing the static boxplot
carfig2

# Converting the boxplot into an interactive plot
# Displaying only the custom hover text
ggplotly(carfig2, tooltip = "text") # Specifying interactive feature
# Additional tooltip options include: "x", "y", "fill", and "colour"

4 Is it helpful?

4.1 Yes!

4.1.1 Community Health & Emergency Response Coordinator at Latino Network

  • I train Community Health Workers (CHWs) to become certified with OHA
  • I also do workshops with Latinx community members regarding emergency preparedness, commercial tobacco cessation, & overdose prevention
  • Using ggplotly() could potentially be a fun & interactive way to present data to community members & CHWs

4.1.2 Epidemiology

  • I would create visualizations using this function for an interactive research presentation, maybe even a digital research poster