ggplot2 scale_y_continuous() in R: Customize the Y Axis
The scale_y_continuous() function in ggplot2 customizes the Y axis on plots with a continuous (numeric) y mapping. It is the y-axis sister of scale_x_continuous().
+ scale_y_continuous(breaks = seq(0, 100, 10)) + scale_y_continuous(labels = scales::dollar) + scale_y_continuous(limits = c(0, 50)) + scale_y_continuous(trans = "log10") + scale_y_continuous(name = "Revenue ($)")
Need explanation? Read on for examples and pitfalls.
What scale_y_continuous() does in one sentence
scale_y_continuous() controls the Y axis: breaks (tick positions), labels (tick text), limits (range), transformations. Identical to scale_x_continuous but for y.
Syntax
scale_y_continuous(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, trans = "identity", sec.axis = waiver(), ...).
scales::dollar / scales::comma / scales::percent for common label formats. They're functions, not strings.Five common patterns
1. Custom breaks
2. Dollar-formatted labels
3. Percent labels
4. Log scale
5. Secondary y axis
scale_y_continuous() vs scale_y_log10() vs coord_cartesian
| Function | Best for |
|---|---|
scale_y_continuous() |
General y customization |
scale_y_log10() |
Shortcut for log y |
scale_y_sqrt() |
Square-root y |
coord_cartesian(ylim) |
Zoom without dropping data |
A practical workflow
Combine breaks, labels, and trans for production-ready axes.
Dollar formatting + breaks + tight bottom expansion.
Common pitfalls
Pitfall 1: limits drops data. Use coord_cartesian(ylim = c(0, 100)) to zoom without dropping.
Pitfall 2: dual axes can mislead. ggplot's sec.axis allows it but only with a linear transformation; don't combine unrelated scales.
scale_y_continuous(limits = ...) DROPS data outside; coord_cartesian(ylim = ...) ZOOMS only. Use coord_cartesian when stats need the full data.Try it yourself
Try it: Plot mtcars wt vs hp with y axis log-transformed and comma labels. Save to ex_plot.
Click to reveal solution
Explanation: scale_y_log10 with comma formatting.
Related ggplot2 functions
After mastering scale_y_continuous, look at:
scale_x_continuous(): same for xscale_y_log10()/scale_y_sqrt(): shortcutsscale_y_date(): time-axis yscale_y_discrete(): discrete ycoord_cartesian(ylim): zoom onlyexpand/expansion(): control axis padding
FAQ
What does scale_y_continuous do in ggplot2?
scale_y_continuous() customizes the Y axis when y is continuous numeric: breaks, labels, limits, transformations.
How do I add a secondary y axis?
Pass sec.axis = sec_axis(~ . / 100, name = "..."). The transformation must be linear.
What is the difference between scale_y_continuous limits and coord_cartesian?
limits drops data outside. coord_cartesian zooms only. Use coord_cartesian for stats integrity.
How do I format y as dollars?
scale_y_continuous(labels = scales::dollar). Use comma, percent similarly.
Can I log-transform with scale_y_continuous?
Yes: scale_y_continuous(trans = "log10"). Or use scale_y_log10() as a shortcut.