library(ggplot2)
|>
mtcars ggplot() +
aes( x= mpg, y = wt, color = factor(cyl)) +
geom_point() +
facet_grid(.~cyl) +
theme(
# Plot title, subtitle, caption, and tag
plot.title = element_text(size = 18, face = "bold", color = "blue", hjust = 0.5),
plot.subtitle = element_text(size = 14, face = "italic", color = "darkblue"),
plot.caption = element_text(size = 10, color = "gray50"),
plot.tag = element_text(size = 16, face = "bold", color = "red"),
# Axis titles
axis.title.x = element_text(size = 14, face = "bold", color = "purple"),
axis.title.y = element_text(size = 14, face = "bold", color = "purple"),
# Axis text (tick labels)
axis.text.x = element_text(size = 12, color = "black", angle = 45, vjust = 1),
axis.text.y = element_text(size = 12, color = "black"),
# Legend title and text
legend.title = element_text(size = 14, face = "bold", color = "darkred"),
legend.text = element_text(size = 12, color = "black"),
# Facet strip text
strip.text.x = element_text(size = 14, face = "bold", color = "darkgreen"),
strip.text.y = element_text(size = 14, face = "bold", color = "darkgreen")
+
) labs(
title = "Relationship between MPG and Weight",
subtitle = "Colored by Number of Cylinders",
caption = "Source: mtcars dataset",
tag = "A"
)
ggplot2: Modified Plot
theme() function
The theme()
function in ggplot2 allows you to customize various aspects of a plot’s appearance. Below is a categorized list of arguments in theme()
:
1. Text Elements
These control text appearance in different parts of the plot.
title
: Title of the plot.plot.title
: Main plot title.plot.subtitle
: Subtitle of the plot.plot.caption
: Caption (text at the bottom of the plot).axis.title
: Titles of both axes.axis.title.x
axis.title.y
axis.text
: Labels of the axes.axis.text.x
axis.text.y
legend.title
: Legend title.legend.text
: Legend labels.strip.text
: Facet labels (for faceted plots).strip.text.x
strip.text.y
element_text()
The element_text()
function in ggplot2 is used within the theme()
function to customize text elements like titles, axis labels, and legend text.
The list of arguments:
- Font Size & Style
family
→ Font family (e.g.,"serif"
,"sans"
,"mono"
)face
→ Font style"plain"
(default)"bold"
"italic"
"bold.italic"
size
→ Font size (in points)lineheight
→ Line spacing (multiplier, default is1.2
)
- Color & Transparency
color
→ Text color (e.g.,"red"
,"blue"
,"#123456"
)hjust
→ Horizontal justification (0 = left
,0.5 = center
,1 = right
)vjust
→ Vertical justification (0 = bottom
,0.5 = middle
,1 = top
)alpha
→ Transparency (0 = fully transparent
,1 = fully opaque
)
- Position Adjustments
angle
→ Rotate text (degrees, e.g.,45
,90
,-45
)margin
→ Add margins around text (margin(top, right, bottom, left)
)
Summary
Argument | Description | Example |
---|---|---|
family |
Font family (e.g., "serif" , "sans" ) |
family = "serif" |
face |
Font style ("bold" , "italic" , etc.) |
face = "bold" |
size |
Font size (in points) | size = 14 |
lineheight |
Line spacing | lineheight = 1.5 |
color |
Text color | color = "red" |
alpha |
Transparency (0 to 1 ) |
alpha = 0.7 |
hjust |
Horizontal justification (0 to 1 ) |
hjust = 0.5 |
vjust |
Vertical justification (0 to 1 ) |
vjust = 1 |
angle |
Rotate text (degrees) | angle = 45 |
margin |
Margin around text | margin(5,5,5,5) |
Summary of element_text()
Usage in theme()
theme() Argument |
Description |
---|---|
plot.title |
Main title |
plot.subtitle |
Subtitle |
plot.caption |
Caption at bottom |
plot.tag |
Tag (e.g., “A”, “B”) |
axis.title.x |
X-axis title |
axis.title.y |
Y-axis title |
axis.text.x |
X-axis tick labels |
axis.text.y |
Y-axis tick labels |
legend.title |
Legend title |
legend.text |
Legend labels |
strip.text.x |
Facet labels (horizontal) |
strip.text.y |
Facet labels (vertical) |
2. Background & Panel Elements
These control the background, grid, and panel appearance.
plot.background
: Background of the entire plot.panel.background
: Background of the plotting area.panel.grid
: Grid lines.panel.grid.major
panel.grid.minor
panel.border
: Border around the plot panel.panel.spacing
: Spacing between facets.
3. Axis Customization
These control the appearance of axes and ticks.
axis.line
: Axis lines.axis.ticks
: Ticks on the axes.axis.ticks.x
axis.ticks.y
axis.text
: Axis labels.axis.text.x
axis.text.y
axis.title
: Axis titles.axis.title.x
axis.title.y
4. Legend Elements
These control the appearance of the legend.
legend.background
: Background of the legend.legend.key
: Background of legend keys.legend.key.size
: Size of legend keys.legend.position
: Position of the legend."none"
,"left"
,"right"
,"top"
,"bottom"
legend.direction
: Layout direction ("horizontal"
or"vertical"
).legend.box
: Arrangement of multiple legends ("horizontal"
or"vertical"
).
5. Facet Customization
These control the appearance of facet labels.
strip.background
: Background color of facet labels.strip.text
: Text of facet labels.strip.text.x
strip.text.y
strip.placement
: Placement of facet labels ("inside"
or"outside"
).
6. Margins & Spacing
These control spacing between different elements.
plot.margin
: Margins around the plot.legend.margin
: Margins around the legend.
element_rect()
function
The element_rect()
function in ggplot2 is used within the theme()
function to customize rectangular elements like backgrounds, borders, and legend keys.
The list of available arguments:
1. Fill & Border Color
fill
→ Background color (e.g.,"red"
,"blue"
,"#123456"
,NA
for transparent)colour
/color
→ Border color (same format asfill
)
2. Border Line Settings
linewidth
→ Border line thickness (default0.5
)linetype
→ Border line type:1
or"solid"
→ Solid line2
or"dashed"
→ Dashed line3
or"dotted"
→ Dotted line4
or"dotdash"
→ Dot-dash line5
or"longdash"
→ Long dash6
or"twodash"
→ Two dashes
3. Rounded Corners
r
→ Corner radius (default0
, set higher for rounded corners)
Summary of element_rect()
Arguments
Argument | Description | Example |
---|---|---|
fill |
Background color | fill = "gray90" |
color / colour |
Border color | colour = "black" |
linewidth |
Border thickness | linewidth = 1.5 |
linetype |
Border line type | linetype = "dashed" |
r |
Corner radius (rounded corners) | r = 5 |
Summary of element_rect()
Usage in theme()
theme() Argument |
Description |
---|---|
plot.background |
Entire plot background |
panel.background |
Plotting area background |
panel.border |
Border around the plotting area |
legend.background |
Background behind the legend |
legend.box.background |
Border around the legend box |
legend.key |
Background of legend keys |
strip.background |
Background of facet labels |
|>
mtcars ggplot() +
aes(x = mpg, y = wt, color = factor(cyl)) +
geom_point(size = 3) +
facet_grid(.~cyl) +
theme(
# Background settings
plot.background = element_rect(fill = "gray90", color = "black", linewidth = 2),
panel.background = element_rect(fill = "white", color = "black", linewidth = 1),
# Borders
panel.border = element_rect(color = "red", fill = NA, linewidth = 1.5),
# Legend
legend.background = element_rect(fill = "lightblue", color = "black", linewidth = 1),
legend.box.background = element_rect(fill = "gray80", color = "black", linewidth = 1.5),
legend.key = element_rect(fill = "white", color = "black", linewidth = 0.5),
# Facet strip (for facet_wrap)
strip.background = element_rect(fill = "yellow", color = "black", linewidth = 1)
+
) labs(
title = "MPG vs Weight with Custom Theme",
subtitle = "Customized with element_rect()"
)
element_line()
function
The element_line()
function in ggplot2 is used within theme()
to customize lines in a plot, such as axis lines, grid lines, and borders.
Arguments in element_line()
The list of available arguments:
1. Line Appearance
colour
/color
→ Line color (e.g.,"black"
,"red"
,"#123456"
)size
→ Line thickness (default is0.5
)linetype
→ Line type:1
or"solid"
→ Solid line2
or"dashed"
→ Dashed line3
or"dotted"
→ Dotted line4
or"dotdash"
→ Dot-dash line5
or"longdash"
→ Long dash6
or"twodash"
→ Two dashes
lineend
→ Line end style:"round"
(default)"butt"
"square"
2. Transparency
alpha
→ Transparency (0 = fully transparent
,1 = fully opaque
)
Summary of element_line()
Arguments
Argument | Description | Example |
---|---|---|
colour / color |
Line color | color = "red" |
size |
Line thickness | size = 1 |
linetype |
Line type (solid, dashed, etc.) | linetype = "dotted" |
lineend |
Line end style (round, butt, square) | lineend = "round" |
alpha |
Transparency (0 to 1 ) |
alpha = 0.7 |
Summary of element_line()
Usage in theme()
theme() Argument |
Description |
---|---|
axis.line.x |
Line along the x-axis |
axis.line.y |
Line along the y-axis |
panel.grid.major.x |
Major grid lines along the x-axis |
panel.grid.major.y |
Major grid lines along the y-axis |
panel.grid.minor.x |
Minor grid lines along the x-axis |
panel.grid.minor.y |
Minor grid lines along the y-axis |
strip.switch.pad.grid |
Line between facet strips (grid layout) |
strip.switch.pad.wrap |
Line between wrapped facets |
|>
mtcars ggplot() +
aes(mpg, wt, color = factor(cyl)) +
geom_point(size = 3) +
theme(
# Axis lines
axis.line.x = element_line(color = "black", linewidth = 1, linetype = "solid"),
axis.line.y = element_line(color = "black", linewidth = 1, linetype = "solid"),
# Major grid lines
panel.grid.major.x = element_line(color = "gray", linewidth = 0.5, linetype = "dashed"),
panel.grid.major.y = element_line(color = "gray", linewidth = 0.5, linetype = "dotted"),
# Minor grid lines
panel.grid.minor.x = element_line(color = "lightgray", linewidth = 0.3, linetype = "dashed"),
panel.grid.minor.y = element_line(color = "lightgray", linewidth = 0.3, linetype = "dotted"),
# Facet strip separators
strip.switch.pad.grid = unit(0.5, "cm"),
strip.switch.pad.wrap = unit(0.5, "cm")
+
) labs(
title = "MPG vs Weight with Custom Line Styling",
subtitle = "Customized with element_line()"
)
element_blank()
function
The element_blank()
function in ggplot2 is used within theme()
to completely remove an element from a plot.
element_blank()
removes an element completely.Useful for minimalist plots.
Works inside
theme()
with axis labels, titles, grid lines, legends, backgrounds, etc..
Summary of element_blank()
Usage in theme()
theme() Argument |
Description |
---|---|
axis.text.x / axis.text.y |
Remove axis text labels |
axis.title.x / axis.title.y |
Remove axis titles |
axis.line.x / axis.line.y |
Remove axis lines |
axis.ticks.x / axis.ticks.y |
Remove axis ticks |
panel.grid.major / panel.grid.minor |
Remove grid lines |
plot.background |
Remove the entire plot background |
panel.background |
Remove the background of the plotting area |
panel.border |
Remove the border around the plot panel |
legend.background |
Remove the legend background |
legend.key |
Remove the background of legend items |
legend.title |
Remove the legend title |
legend.text |
Remove the legend text |
strip.background |
Remove the background of facet labels |
strip.text |
Remove the text inside facet labels |
annotae() function
The annotate()
function in ggplot2 allows you to add text, shapes, or other graphical elements to a plot. It is particularly useful for adding annotations that are not tied to data points but rather placed at specific coordinates.
Syntax of annotate()
annotate(geom,
label = NULL,
x, y, color = "black", size = 5, ...)
geom
→ Type of annotation ("text"
,"label"
,"rect"
,"segment"
,"point"
, etc.).x
,y
→ Position coordinates for the annotation.label
→ Text to be displayed (ifgeom = "text"
orgeom = "label"
).color
→ Color of the annotation.size
→ Size of text or shape.Additional parameters depending on the
geom
.
Examples of annotate()
Usage
1️) Adding Text Annotation
library(ggplot2)
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "text",
x = 25, y = 4,
label = "Fuel Efficient", color = "red", size = 5)
🔹 Adds a red text label at (x = 25, y = 4)
.
2️) Adding a Label with a Background
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "label",
x = 15, y = 5,
label = "Heavy Car", fill = "yellow",
color = "black", size = 5)
🔹 Adds a label with a yellow background.
3️) Adding a Rectangle
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "rect",
xmin = 20, xmax = 30, ymin = 2, ymax = 4,
alpha = 0.2,
fill = "blue")
🔹 Draws a semi-transparent blue rectangle.
4️) Adding a Segment (line, arrow)
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "segment",
x = 10, xend = 30, y = 5, yend = 2,
color = "blue", size = 1, linetype = "dashed")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
🔹 Adds a dashed blue line from (10, 5)
to (30, 2)
.
Adding an Arrow Annotation
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "segment",
x = 15, xend = 25, # Start and end X coordinates
y = 5, yend = 3, # Start and end Y coordinates
color = "red",
size = 1,
arrow = arrow(type = "closed", length = unit(0.3, "cm")) # Arrow style
)
🔹 This adds a red arrow pointing from (15,5)
to (25,3)
.
Customize the Arrow
We can change:
Arrow type:
"open"
,"closed"
, or"both"
Arrow length:
unit(0.5, "cm")
Line type:
linetype = "dashed"
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "segment",
x = 10, xend = 30,
y = 4, yend = 2,
color = "blue",
size = 1,
linetype = "dashed",
arrow = arrow(type = "open", length = unit(0.5, "cm"))
)
🔹 This adds a dashed blue arrow with an open arrowhead.
5️) Adding a Point
|>
mtcars ggplot() +
aes(x = mpg, y = wt) +
geom_point() +
annotate(geom = "point",
x = 25, y = 3,
color = "red", size = 5, shape = 8)
🔹 Adds a red star-shaped point at (25,3)
.
When to Use annotate()
?
✅ When you want to add static text or shapes that are not dependent on data.
✅ When you want to highlight specific areas of a plot.
✅ When you need annotations that stay in place even if the data changes.