Introduction
ggplot2 is R's most popular visualization package based on the Grammar of Graphics. It creates elegant, customizable plots.
Basic Structure
library(ggplot2)
ggplot(data, aes(x = variable1, y = variable2)) +
geom_point()
Quick Plots
# Using qplot
qplot(x, y, data = df)
qplot(x, data = df, geom = "histogram")
Core Components
ggplot(df, aes(x = var1, y = var2)) +
# Data
# Aesthetics (aes)
# Geometries (geom)
geom_point()
# Labels
labs(title = "Title", x = "X", y = "Y")
# Theme
theme_minimal()
Aesthetic Mappings
ggplot(df, aes(x = var1, y = var2,
color = category,
size = value,
shape = type,
alpha = 0.5)) +
geom_point()
Summary
ggplot2 provides a powerful grammar for creating visualizations. Build plots layer by layer.