How to show multiple ggplot2 plots with Plotly using R?

R is a programming language for statistical computing and graphics. ggplotly() is a function that converts static ggplot2 plots into interactive web-based visualizations. When combined with facet_grid(), you can display multiple related plots in a single interactive dashboard.

Prerequisites

First, install the required R packages ?

install.packages('ggplot2')
install.packages('plotly')
install.packages('dplyr')

Method 1: Using facet_grid() with External Data

This approach creates multiple plots by splitting data across different categories using facet_grid() ?

library(ggplot2)
library(plotly)
library(dplyr)

# Create sample student data
students_data <- data.frame(
  language = c(85, 92, 78, 88, 95, 82, 90, 87, 93, 89,
               76, 84, 91, 86, 88, 94, 79, 85, 92, 87),
  state = c(rep("NY", 10), rep("CA", 10))
)

# Create ggplot with histogram
p1 <- ggplot(students_data, aes(x = language)) + 
      geom_histogram(color = 'blue', fill = 'lightblue', bins = 8) +
      labs(title = "Student Language Scores by State", 
           x = "Language Score", y = "Frequency")

# Add faceting to create multiple plots
figure <- p1 + facet_grid(rows = vars(state))

# Convert to interactive plotly
ggplotly(figure)

Method 2: Using subplot() for Different Plot Types

Create multiple different plot types and combine them using subplot() ?

library(ggplot2)
library(plotly)

# Sample data
data <- data.frame(
  x = 1:10,
  y1 = rnorm(10, 50, 10),
  y2 = rnorm(10, 30, 5),
  category = rep(c("A", "B"), each = 5)
)

# Create different plot types
p1 <- ggplot(data, aes(x = x, y = y1)) + 
      geom_point(color = "red") + 
      labs(title = "Scatter Plot")

p2 <- ggplot(data, aes(x = category, y = y2)) + 
      geom_boxplot(fill = "lightgreen") + 
      labs(title = "Box Plot")

# Convert to plotly and combine
plot1 <- ggplotly(p1)
plot2 <- ggplotly(p2)

subplot(plot1, plot2, nrows = 1)

Method 3: Using facet_wrap() for Flexible Layouts

facet_wrap() provides more flexible arrangements compared to facet_grid() ?

library(ggplot2)
library(plotly)

# Create sample sales data
sales_data <- data.frame(
  month = rep(month.abb[1:6], 4),
  sales = c(100, 120, 140, 160, 180, 200,
            110, 130, 150, 170, 190, 210,
            90, 110, 130, 150, 170, 190,
            120, 140, 160, 180, 200, 220),
  region = rep(c("North", "South", "East", "West"), each = 6)
)

# Create plot with facet_wrap
p <- ggplot(sales_data, aes(x = month, y = sales, group = 1)) +
     geom_line(color = "blue", size = 1) +
     geom_point(color = "red", size = 2) +
     facet_wrap(~region, ncol = 2) +
     labs(title = "Sales by Region", x = "Month", y = "Sales")

# Convert to interactive plot
ggplotly(p)

Key Functions

Function Purpose Best For
facet_grid() Creates grid of plots Comparing across two variables
facet_wrap() Wraps plots in flexible layout Single grouping variable
subplot() Combines different plot objects Different plot types together
ggplotly() Converts to interactive plot Adding interactivity

Conclusion

Use facet_grid() or facet_wrap() to create multiple related plots from the same dataset. Use subplot() to combine different plot types. Always convert with ggplotly() for interactive web-based visualization.

Updated on: 2026-03-26T22:33:06+05:30

611 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements