ggplot2 scale_x_continuous() in R: Customize the X Axis
The scale_x_continuous() function in ggplot2 customizes the X axis on plots with a continuous (numeric) x mapping. It controls breaks, labels, limits, and transformations.
+ scale_x_continuous(breaks = seq(0, 100, 10)) + scale_x_continuous(labels = scales::comma) + scale_x_continuous(limits = c(0, 50)) + scale_x_continuous(trans = "log10") + scale_x_continuous(name = "Time (sec)")
Need explanation? Read on for examples and pitfalls.
What scale_x_continuous() does in one sentence
scale_x_continuous() controls the X axis on plots with continuous numeric x: breaks (tick positions), labels (tick text), limits (min/max), and transformations (log, sqrt, etc.).
Syntax
scale_x_continuous(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), trans = "identity", ...).
scales::comma, scales::dollar, scales::percent for common label formats. They're functions, not strings, pass without parens.Five common patterns
1. Custom breaks
2. Comma-formatted labels
3. Limits
4. Log transformation
scale_x_log10() is a shortcut.
5. Custom labels with formatter
limits = DROPS data outside the range; coord_cartesian(xlim =) ZOOMS WITHOUT dropping. This matters for stats: dropped data isn't included in fits or summaries.scale_x_continuous() vs scale_x_log10() vs coord_cartesian()
| Function | Best for |
|---|---|
scale_x_continuous() |
General x-axis customization |
scale_x_log10() |
Shortcut for log scale |
scale_x_date() |
Date x axis |
coord_cartesian(xlim) |
Zoom without dropping data |
A practical workflow
Use scales package functions for label formatters.
Month names on x; dollar amounts on y.
Common pitfalls
Pitfall 1: limits drops data. scale_x_continuous(limits = c(0, 50)) excludes points outside; downstream stats see fewer points. Use coord_cartesian(xlim = c(0, 50)) to zoom only.
Pitfall 2: passing string to labels. labels = "comma" errors. Pass the function: labels = scales::comma (no parens).
scale_x_continuous REPLACES the default x scale. Adding it twice in a plot just keeps the last one (with a warning).Try it yourself
Try it: Plot mtcars disp vs mpg with x-axis breaks at 100, 200, 300, 400, 500 and comma-formatted labels. Save to ex_plot.
Click to reveal solution
Explanation: Custom breaks at 100s; comma formatter for readability.
Related ggplot2 functions
After mastering scale_x_continuous, look at:
scale_y_continuous(): same for y axisscale_x_log10()/scale_x_sqrt(): shortcutsscale_x_date()/scale_x_datetime(): time axesscale_x_discrete(): discrete xcoord_cartesian(): zoom without dropping datascales::comma()/dollar()/percent(): label formatters
FAQ
What does scale_x_continuous do in ggplot2?
scale_x_continuous() customizes the X axis when x is a continuous numeric variable: breaks, labels, limits, transformations.
What is the difference between scale_x_continuous limits and coord_cartesian?
limits drops data outside the range. coord_cartesian zooms without dropping. Use coord_cartesian if you need stats over the full data.
How do I format axis labels with commas?
scale_x_continuous(labels = scales::comma). The scales package has comma, dollar, percent, and other formatters.
How do I log-transform the x axis?
scale_x_log10() is the shortcut. Or scale_x_continuous(trans = "log10").
Can I use it with discrete x?
No. For factor / character x, use scale_x_discrete().