ggplot2 scale_y_log10() in R: Log-Transform the Y Axis
The scale_y_log10() function in ggplot2 log10-transforms the Y axis. It is the y-axis sister of scale_x_log10() and essential for visualizing skewed dependent variables.
+ scale_y_log10()
+ scale_y_log10(labels = scales::comma)
+ scale_y_log10(breaks = c(1, 10, 100, 1000))
+ scale_y_continuous(trans = "log10") # equivalent
+ scale_y_log10(labels = scales::dollar)Need explanation? Read on for examples and pitfalls.
What scale_y_log10() does in one sentence
scale_y_log10() log10-transforms the Y axis values, equivalent to scale_y_continuous(trans = "log10"). Useful for skewed positive y values like revenue, prices, or counts.
Syntax
scale_y_log10(name = waiver(), breaks = waiver(), labels = waiver(), ...). Same as scale_y_continuous.
scale_y_log10 often reveals patterns that linear y hides. Skewed positive data is the natural use case.Five common patterns
1. Standard log y
2. Dollar-formatted log labels
3. Custom breaks
4. Combined with x log
5. Pseudo-log for zero values
pseudo_log handles 0 and negatives; pure log10 cannot.
scale_y_log10() vs scale_y_continuous(trans=) vs pseudo_log
| Function | Transform | Handles 0 |
|---|---|---|
scale_y_log10() |
Log10 | No |
scale_y_continuous(trans = "log10") |
Same | No |
scale_y_sqrt() |
Sqrt | Yes |
scales::pseudo_log_trans() |
Log near zeros, linear at 0 | Yes |
A practical workflow
For skewed y, log10 is the default. For data with zeros, pseudo_log is a safer fallback.
For revenue charts:
Common pitfalls
Pitfall 1: zeros and negatives drop. Log fails on non-positive y. Use pseudo_log_trans or filter zeros.
Pitfall 2: misleading bar charts. Bar charts on log y axis distort proportions; readers easily misinterpret. Avoid log y on bar charts when possible.
scale_y_log10() transforms the data BEFORE stats run. Regression / smoothers fit on log values. If you only want log VISUAL, use coord_trans(y = "log10") instead.Try it yourself
Try it: Plot mtcars hp vs disp on log y. Save to ex_plot.
Click to reveal solution
Explanation: Log10 transforms the y axis.
Related ggplot2 / scales functions
After mastering scale_y_log10, look at:
scale_x_log10(): same for xscale_y_sqrt(): square root ycoord_trans(y = "log10"): visual-only transformscales::pseudo_log_trans(): handles zeroscales::label_log(): 10^N labels
FAQ
What does scale_y_log10 do in ggplot2?
scale_y_log10() log10-transforms the Y axis. Shortcut for scale_y_continuous(trans = "log10").
Can I use scale_y_log10 with zero values?
No. Log fails on 0 and negatives. Use scale_y_continuous(trans = scales::pseudo_log_trans()) to handle zeros.
What is the difference between scale_y_log10 and coord_trans(y = "log10")?
scale_y_log10 transforms data before stats. coord_trans transforms only the visual after stats. Different for regression and smoothers.
Why does my bar chart on log y look weird?
Log scales distort bar proportions. Readers misinterpret heights. Avoid log y on bars; use it for points and lines.
Can I add log y on top of an existing plot?
Yes. + scale_y_log10() after the geoms. Or chain with other scale modifications.