Advanced ggplot2 & plotly
R Programming & Data Analytics / Advanced ggplot2 & plotly

Advanced ggplot2 & plotly

Intermediate 10 hrs 3 Concepts
M1

Small Multiples

Concept 1

Faceting with facet_wrap and facet_grid

facet_wrap() wraps 1D panels; facet_grid() creates a 2D grid of panels. Both produce 'small multiples' — the most powerful EDA technique.

R
ggplot(df, aes(x=score)) + geom_histogram(binwidth=5) + facet_wrap(~subject, nrow=2)
ggplot(df, aes(x=score, y=grade)) + geom_point() + facet_grid(year~subject)
Solved Examples
Example 1 Apply the concept of Faceting with facet_wrap and facet_grid to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. What is the primary purpose of faceting with facet_wrap and facet_grid?
Q2. Which R package is most relevant for this topic?
M2

Interactive Charts

Concept 1

plotly Integration

ggplotly() converts any ggplot2 chart to an interactive Plotly chart in one line.

R
library(plotly)
p <- ggplot(mtcars, aes(x=wt, y=mpg, text=rownames(mtcars))) + geom_point()
ggplotly(p, tooltip=c('text','x','y'))
# Click, zoom, pan, hover — all built in
Solved Examples
Example 1 Apply the concept of plotly Integration to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. What is the primary purpose of plotly integration?
Q2. Which R package is most relevant for this topic?
Concept 2

Combining Plots with patchwork

patchwork lets you combine multiple ggplot2 charts with + and / operators.

R
library(patchwork)
p1 + p2            # side by side
p1 / p2            # stacked
(p1 + p2) / p3     # top row + bottom
Solved Examples
Example 1 Apply the concept of Combining Plots with patchwork to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. What is the primary purpose of combining plots with patchwork?
Q2. Which R package is most relevant for this topic?
Data Visualisation with ggplot2 Statistical Analysis in R