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.

⚡ Quick Answer
+ 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.

📊 Is scale_x_continuous() the right tool?
STARTnumeric x axis customizationscale_x_continuous()log-scale xscale_x_log10() (shortcut)date xscale_x_date()discrete xscale_x_discrete()y-axis equivalentscale_y_continuous()

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", ...).

Run live
Run live, no install needed. Every R block on this page runs in your browser. Click Run, edit the code, re-run instantly. No setup.
RCustom breaks and comma labels
library(ggplot2) library(scales) ggplot(mtcars, aes(disp, mpg)) + geom_point() + scale_x_continuous( breaks = seq(0, 500, 100), labels = comma )

  
Tip
Use scales::comma, scales::dollar, scales::percent for common label formats. They're functions, not strings, pass without parens.

Five common patterns

1. Custom breaks

RTick every 10 units
+ scale_x_continuous(breaks = seq(0, 100, 10))

  

2. Comma-formatted labels

RBig numbers readable
+ scale_x_continuous(labels = scales::comma)

  

3. Limits

RZoom to a range
+ scale_x_continuous(limits = c(0, 50)) #> NOTE: limits drops data outside; use coord_cartesian(xlim) to zoom only

  

4. Log transformation

RLog-scale x
+ scale_x_continuous(trans = "log10", labels = scales::comma)

  

scale_x_log10() is a shortcut.

5. Custom labels with formatter

RPercent labels
+ scale_x_continuous(labels = scales::percent)

  
Key Insight
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.

RInteractive R
library(scales) ggplot(sales, aes(month, revenue)) + geom_col() + scale_x_continuous(breaks = 1:12, labels = month.abb) + scale_y_continuous(labels = dollar)

  

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).

Warning
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.

RYour turn: x axis customization
library(scales) ex_plot <- mtcars |> ggplot(aes(disp, mpg)) + geom_point() + # your code here

  
Click to reveal solution
RSolution
ex_plot <- mtcars |> ggplot(aes(disp, mpg)) + geom_point() + scale_x_continuous(breaks = seq(100, 500, 100), labels = comma)

  

Explanation: Custom breaks at 100s; comma formatter for readability.

After mastering scale_x_continuous, look at:

  • scale_y_continuous(): same for y axis
  • scale_x_log10() / scale_x_sqrt(): shortcuts
  • scale_x_date() / scale_x_datetime(): time axes
  • scale_x_discrete(): discrete x
  • coord_cartesian(): zoom without dropping data
  • scales::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().