Examples using the scales package with ggplot2

The scales package contains functions for scaling adjustments used in ggplot2. This package helps effectively adjust various properties of the graph such as axes, colors, and sizes. The following examples will help you understand the scales functions better.

ggplot(tibble(x = c(-1e6, 1e6))) +
  geom_blank(aes(x,1))

ggplot(tibble(x = c(-1e6, 1e6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_number())

ggplot(tibble(x = c(-1e6, 1e6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_comma())

ggplot(tibble(x = c(-1e6, 1e6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_comma(style_positive = "plus"))

ggplot(tibble(x = c(-1e6, 1e6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_comma(style_negative = "parens"))

ggplot(tibble(x = c(0, 1e6))) +
  geom_blank(aes(x,1)) 

ggplot(tibble(x = c(0, 1e6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_number(scale = 1/1000))

ggplot(tibble(x = c(0, 1e-6))) +
  geom_blank(aes(x,1)) 

ggplot(tibble(x = c(0, 1e-6))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_number(scale = 1e6))

ggplot(tibble(x = c(0, 1e9))) +
  geom_blank(aes(x,1))

ggplot(tibble(x = c(0, 1e9))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_number(scale_cut = cut_short_scale()))

ggplot(tibble(x = c(1, 1e9))) +
  geom_blank(aes(x,1)) +
  scale_x_log10(breaks = log_breaks(10),
                labels = label_number(scale_cut = cut_short_scale()))

ggplot(tibble(x = c(1, 1e9))) +
  geom_blank(aes(x,1)) +
  scale_x_log10(breaks = log_breaks(10),
                labels = label_number(scale_cut = cut_si('g')))

ggplot(tibble(x = c(1, 1e9))) +
  geom_blank(aes(x,1)) +
  scale_x_log10(breaks = log_breaks(10),
                labels = label_number(scale_cut = cut_si('m')))

ggplot(tibble(x = c(0, 100))) +
  geom_blank(aes(x,1)) +
  scale_x_continuous(labels = label_number(suffix = "°C"))


See also