ggplot2 Legends and Axes: Customize Scales and Guides
Scales control how data values are translated into visual positions, colours and sizes. Guides, including legends and axes, explain those mappings to readers.
This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.
Change axis titles and breaks
labs() changes axis titles. Scale functions control tick positions and labels.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_x_continuous(
breaks = seq(2, 7, by = 1)
) +
scale_y_continuous(
breaks = seq(10, 40, by = 5)
)
Set axis labels and limits carefully
Use limits when values outside a range should be removed from the scale. Use coord_cartesian() to zoom the visible area without dropping observations before statistical calculations.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
coord_cartesian(
xlim = c(2, 6),
ylim = c(10, 40)
)
Rename a legend
Use labs() with the aesthetic name to change a legend title. The name can also be set in a scale function.
ggplot(mpg, aes(displ, hwy,
colour = class)) +
geom_point() +
labs(colour = "Vehicle class")
Remove or reposition legends
Remove one guide with guides(), or all legends with theme(legend.position = "none"). Set a position to top or bottom for compact layouts.
ggplot(mpg, aes(displ, hwy,
colour = class)) +
geom_point() +
theme(legend.position = "bottom")
# Remove all legends:
# + theme(legend.position = "none")
Format date and discrete axes
Use scale_x_date() for Date values, scale_x_datetime() for date-times and scale_x_discrete() for categories. Labels can be formatted to make long or dense axes easier to read.
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
scale_x_discrete(
labels = c(
"2seater" = "Two seater",
"subcompact" = "Subcompact"
)
)
Common mistakes and troubleshooting
- Could not find function: run
library(ggplot2)in the current R session. Install it first withinstall.packages("ggplot2")if needed. - Object not found: check spelling and confirm the column exists in the data frame. Use
names(mpg)to inspect column names. - Unexpected legend: a variable mapped inside
aes()creates a scale and usually a legend. A fixed setting belongs outsideaes(). - Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call
print(p)when displaying a stored ggplot object. - Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.
Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.
ggplot2 FAQ
How do I remove a legend in ggplot2?
Use guides(colour = "none") for one aesthetic or theme(legend.position = "none") to remove the entire legend.
How do I change axis breaks?
Use scale_x_continuous(breaks = ...) or scale_y_continuous(breaks = ...) to specify the tick positions.
How do I change a legend title?
Use labs(colour = "Group") or the relevant scale function name, such as scale_colour_discrete(name = "Group").