scale_axis: Use a different scale on either axis of a ggplot2 plot.

View source: R/scale_axis.R

scale_axisR Documentation

Use a different scale on either axis of a ggplot2 plot.

Description

scale_axis takes a ggplot2 object as input and applies to its x-axis or y-axis one of the following scales:

  • comma . Show numbers in 000,000 format.

  • dollar. Show $ in front of numbers.

  • pct . Use percent format.

  • log . Use log transformation.

  • log1p . Use log(x+1) transformation.

  • log10 . Use log10 transformation.

  • log2 . Use log2 transformation.

  • sqrt . Use squared root transformation.

  • exp . Use exponential transformation.

  • date . Use date format.

If a ggplot object has too few breaks on an axis (to see the max value), and if you don't need to apply any scales to the axis (due to the values are already in a nice range), you can apply scale='default' to that axis, and this will add nticks (a number supplied by you) from the min to the max value on that axis.

Usage

scale_axis(p, axis = "y", scale = "default", nticks = 10, digits)

Arguments

p

A ggplot2 object.

axis

A string of value "x" or "y". Default = "y".

scale

A string of value "default", "comma", "dollar", "pct", "log", "log1p", "log10", "log2", "sqrt", "exp", or 'date'.

nticks

Number of ticks on axis. Default = 10. The actual number of ticks shown will be 1 or 2 less or more that the number user supplied depends on the actual limit of the data. This is caused by rounding in computation.

digits

Number of digits after the decimal point when using the 'pct' scale. Default uses best guess. It's only effective when ‘scale = ’pct''.

Value

A ggplot2 object with the new scale applied to the input axis.

Examples

inst/examples/ex-scale_axis.R

gmlang/ezplot documentation built on Sept. 18, 2022, 6:33 a.m.