Learn R Programming

⚠️There's a newer version (1.3.0) of this package.Take me there.

scales

One of the most difficult parts of any graphics package is scaling, converting from data values to perceptual properties. The inverse of scaling, making guides (legends and axes) that can be used to read the graph, is often even harder! The scales packages provides the internal scaling infrastructure used by ggplot2, and gives you tools to override the default breaks, labels, transformations and palettes.

Installation

# Scales is installed when you install ggplot2 or the tidyverse.
# But you can install just scales from CRAN:
install.packages("scales")

# Or the development version from Github:
# install.packages("devtools")
devtools::install_github("r-lib/scales")

Usage

Breaks and labels

The most common use of the scales package is to customise to control the appearance of axis and legend labels. Use a break_ function to control how breaks are generated from the limits, and a label_ function to control how breaks are turned in to labels.

library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)

txhousing %>% 
  mutate(date = make_date(year, month, 1)) %>% 
  group_by(city) %>% 
  filter(min(sales) > 5e2) %>% 
  ggplot(aes(date, sales, group = city)) + 
  geom_line(na.rm = TRUE) + 
  scale_x_date(
    NULL,
    breaks = scales::breaks_width("2 years"), 
    labels = scales::label_date("'%y")
  ) + 
  scale_y_log10(
    "Total sales",
    labels = scales::label_number(scale_cut = scales::cut_short_scale())
  )

economics %>% 
  filter(date < ymd("1970-01-01")) %>% 
  ggplot(aes(date, pce)) + 
  geom_line() + 
  scale_x_date(NULL,
    breaks = scales::breaks_width("3 months"), 
    labels = scales::label_date_short()
  ) + 
  scale_y_continuous("Personal consumption expenditures",
    breaks = scales::breaks_extended(8),
    labels = scales::label_dollar()  
  )

Generally, I don’t recommend running library(scales) because when you type (e.g.) scales::label_ autocomplete will provide you with a list of labelling functions to job your memory.

Advanced features

Scales colour palettes are used to power the scales in ggplot2, but you can use them in any plotting system. The following example shows how you might apply them to a base plot.

library(scales)
# pull a list of colours from any palette
viridis_pal()(4)
#> [1] "#440154FF" "#31688EFF" "#35B779FF" "#FDE725FF"

# use in combination with baseR `palette()` to set new defaults
palette(brewer_pal(palette = "Set2")(4))
par(mar = c(5, 5, 1, 1))
plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20)

scales also gives users the ability to define and apply their own custom transformation functions for repeated use.

# use trans_new to build a new transformation
logp3_trans <- trans_new(
  name = "logp",
  transform = function(x) log(x + 3),
  inverse = function(x) exp(x) - 3,
  breaks = log_breaks()
)

dsamp <- sample_n(diamonds, 100)
ggplot(dsamp, aes(carat, price, colour = color)) +
  geom_point() + 
  scale_y_continuous(trans = logp3_trans)

Copy Link

Version

Install

install.packages('scales')

Monthly Downloads

1,353,606

Version

1.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

August 20th, 2022

Functions in scales (1.2.1)

Range

Mutable ranges
alpha

Modify colour transparency
date_breaks

Regularly spaced dates
date_format

Superseded interface to label_date()/label_time()
col_numeric

Colour mapping
col2hcl

Modify standard R colour in hcl colour space.
demo_continuous

Demonstrate scales functions with ggplot2 code
date_trans

Transformation for dates (class Date)
breaks_pretty

Pretty breaks for date/times
dichromat_pal

Dichromat (colour-blind) palette (discrete)
breaks_width

Equally spaced breaks
cscale

Continuous scale
compose_trans

Compose two or more transformations together
colour_ramp

Fast colour interpolation
div_gradient_pal

Diverging colour gradient (continuous).
hms_trans

Transformation for times (class hms)
boxcox_trans

Box-Cox & modulus transformations
format_format

Label using format()
label_dollar

Label currencies ($100, $2.50, etc)
label_number_si

Label numbers with SI prefixes (2 kg, 5 mm, etc)
identity_trans

Identity transformation (do nothing)
identity_pal

Identity palette
label_ordinal

Label ordinal numbers (1st, 2nd, 3rd, etc)
parse_format

Superseded interface to label_parse()/label_math()
comma

Superseded interface to label_number()/label_comma()
fullseq

Generate sequence of fixed size intervals covering range.
brewer_pal

Colour Brewer palette (discrete)
percent_format

Superseded interface to label_percent()
label_log

Label numbers in log format (10^3, 10^6, etc)
linetype_pal

Line type palette (discrete)
label_wrap

Label strings by wrapping across multiple lines
log_trans

Log transformations
exp_trans

Exponential transformation (inverse of log transformation)
oob

Out of bounds handling
ordinal_format

Superseded interface to label_ordinal()
expand_range

Expand a range with a multiplicative or additive constant
shape_pal

Shape palette (discrete)
rescale_pal

Rescale palette (continuous)
grey_pal

Grey scale palette (discrete)
gradient_n_pal

Arbitrary colour gradient palette (continuous)
rescale_none

Don't perform rescaling
show_col

Show colours
cbreaks

Compute breaks for continuous scale
hue_pal

Hue palette (discrete)
rescale

Rescale continuous vector to have specified minimum and maximum
regular_minor_breaks

Minor breaks
label_pvalue

Label p-values (e.g. <0.001, 0.25, p >= 0.99)
label_scientific

Label numbers with scientific notation (e.g. 1e05, 1.5e-02)
wrap_format

Superseded interface to label_wrap()
trans_range

Compute range of transformed values
trans_new

Create a new transformation object
label_number_auto

Label numbers, avoiding scientific notation where possible
minor_breaks_width

Minor breaks
label_number

Label numbers in decimal format (e.g. 0.12, 1,234)
muted

Mute standard colour
yj_trans

Yeo-Johnson transformation
scales-package

scales: Scale Functions for Visualization
reverse_trans

Reverse transformation
probability_trans

Probability transformation
pretty_breaks

Superseded interface to breaks_pretty()
train_continuous

Train (update) a continuous scale
train_discrete

Train (update) a discrete scale
reciprocal_trans

Reciprocal transformation
trans_breaks

Pretty breaks on transformed scale
pvalue_format

Superseded interface to label_pvalue()
trans_format

Format labels after transformation
scientific_format

Superseded interface to label_scientific()
manual_pal

Manual palette (discrete)
dscale

Discrete scale
label_bytes

Label bytes (1 kB, 2 MB, etc)
label_date

Label date/times
dollar_format

Superseded interface to label_dollar()
viridis_pal

Viridis palette
unit_format

Unit labels
seq_gradient_pal

Sequential colour gradient palette (continuous)
label_parse

Label with mathematical annotations
rescale_max

Rescale numeric vector to have specified maximum
rescale_mid

Rescale vector to have specified minimum, midpoint, and maximum
label_percent

Label percentages (2.5%, 50%, etc)
sqrt_trans

Square-root transformation
number

A low-level numeric formatter
time_trans

Transformation for date-times (class POSIXt)
number_bytes_format

Older interface to label_bytes()
zero_range

Determine if range of vector is close to zero, with a specified tolerance
atanh_trans

Arc-tangent transformation
breaks_extended

Automatic breaks for numeric axes
area_pal

Area palettes (continuous)
breaks_log

Breaks for log axes
asn_trans

Arc-sin square root transformation