R ggplot2 Basics
ggplot2 is the most popular visualization package in R. It is built on a system called the Grammar of Graphics — every plot is assembled from layers: data, aesthetic mappings, and geometric shapes. This layered approach makes plots highly customizable and consistent in style.
Installing and Loading ggplot2
install.packages("ggplot2")
library(ggplot2)
The Grammar of Graphics
Every ggplot2 chart has these components: ggplot(data, aes(x, y)) + ← Data and aesthetic mapping geom_*() + ← Geometric layer (what shape to draw) labs() + ← Labels (title, axis names) theme_*() ← Visual theme (background, fonts)
Layer diagram: [Theme] ← background, grid, fonts [Labels] ← title, axis titles, legend [Statistics] ← optional transformations [Geometries] ← bars, points, lines, boxes [Aesthetics] ← x, y, color, size, shape [Data] ← your data frame
Your First ggplot2 Chart
library(ggplot2)
students <- data.frame(
name = c("Asha","Balu","Cena","Dev","Eva"),
score = c(88, 72, 95, 65, 80),
hours = c(5, 3, 7, 2, 6)
)
ggplot(students, aes(x=hours, y=score)) +
geom_point(size=3, color="steelblue") +
labs(title="Study Hours vs Score",
x="Hours Studied",
y="Score") +
theme_minimal()
Aesthetic Mappings — aes()
# Map data columns to visual properties
aes(x=hours, # position on x-axis
y=score, # position on y-axis
color=department, # color by group
size=salary, # size by value
shape=gender, # shape by category
fill=region, # fill color for bars/boxes
alpha=0.7) # transparency (0=invisible, 1=solid)
# Fixed vs mapped aesthetics: geom_point(aes(color=dept)) # mapped — changes with data geom_point(color="red") # fixed — same for all points
Common geom Functions
geom_point() Scatter plot geom_line() Line chart geom_bar() Bar chart (counts) geom_col() Bar chart (values) geom_histogram() Histogram geom_boxplot() Box plot geom_violin() Violin plot geom_smooth() Trend line / regression line geom_text() Text labels on plot geom_hline() Horizontal reference line geom_vline() Vertical reference line
Adding Multiple Layers
ggplot(students, aes(x=hours, y=score)) +
geom_point(size=3, color="steelblue") +
geom_smooth(method="lm", se=FALSE, color="red") + # add regression line
geom_text(aes(label=name), vjust=-1, size=3) + # add name labels
labs(title="Study Hours vs Score",
x="Hours Studied", y="Score",
caption="Source: Class Data") +
theme_minimal()
Faceting — Small Multiples
# Create separate panels per category ggplot(students, aes(x=hours, y=score)) + geom_point() + facet_wrap(~department) # one panel per department # Two-way facet ggplot(data, aes(x=x, y=y)) + geom_point() + facet_grid(gender ~ region) # rows=gender, cols=region
Saving ggplot2 Charts
p <- ggplot(students, aes(x=hours, y=score)) +
geom_point() +
labs(title="Study Chart")
ggsave("study_chart.png", plot=p, width=8, height=5, dpi=300)
ggsave("study_chart.pdf", plot=p, width=8, height=5)
Storing a Plot in a Variable
base_plot <- ggplot(students, aes(x=hours, y=score)) # Add different layers to the same base base_plot + geom_point() base_plot + geom_line() base_plot + geom_smooth()
ggplot2's layered system makes complex charts as easy to build as simple ones — you just add layers. The consistent syntax means every chart type follows the same pattern: data → aesthetics → geometry → labels → theme. Once you learn this structure, every new chart type is just a matter of choosing the right geom.
