Examples for 'ggplot2::scale_x_continuous'


Position scales for continuous data (x & y)

Aliases: scale_x_continuous scale_y_continuous scale_x_log10 scale_y_log10 scale_x_reverse scale_y_reverse scale_x_sqrt scale_y_sqrt

Keywords:

### ** Examples

p1 <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()
p1
plot of chunk example-ggplot2-scale_x_continuous-1
# Manipulating the default position scales lets you:
#  * change the axis labels
p1 +
  scale_x_continuous("Engine displacement (L)") +
  scale_y_continuous("Highway MPG")
plot of chunk example-ggplot2-scale_x_continuous-1
# You can also use the short-cut labs().
# Use NULL to suppress axis labels
p1 + labs(x = NULL, y = NULL)
plot of chunk example-ggplot2-scale_x_continuous-1
#  * modify the axis limits
p1 + scale_x_continuous(limits = c(2, 6))
Warning: Removed 27 rows containing missing values (geom_point).
plot of chunk example-ggplot2-scale_x_continuous-1
p1 + scale_x_continuous(limits = c(0, 10))
plot of chunk example-ggplot2-scale_x_continuous-1
# you can also use the short hand functions `xlim()` and `ylim()`
p1 + xlim(2, 6)
Warning: Removed 27 rows containing missing values (geom_point).
plot of chunk example-ggplot2-scale_x_continuous-1
#  * choose where the ticks appear
p1 + scale_x_continuous(breaks = c(2, 4, 6))
plot of chunk example-ggplot2-scale_x_continuous-1
#  * choose your own labels
p1 + scale_x_continuous(
  breaks = c(2, 4, 6),
  label = c("two", "four", "six")
)
plot of chunk example-ggplot2-scale_x_continuous-1
# Typically you'll pass a function to the `labels` argument.
# Some common formats are built into the scales package:
df <- data.frame(
  x = rnorm(10) * 100000,
  y = seq(0, 1, length.out = 10)
)
p2 <- ggplot(df, aes(x, y)) + geom_point()
p2 + scale_y_continuous(labels = scales::percent)
plot of chunk example-ggplot2-scale_x_continuous-1
p2 + scale_y_continuous(labels = scales::dollar)
plot of chunk example-ggplot2-scale_x_continuous-1
p2 + scale_x_continuous(labels = scales::comma)
plot of chunk example-ggplot2-scale_x_continuous-1
# You can also override the default linear mapping by using a
# transformation. There are three shortcuts:
p1 + scale_y_log10()
plot of chunk example-ggplot2-scale_x_continuous-1
p1 + scale_y_sqrt()
plot of chunk example-ggplot2-scale_x_continuous-1
p1 + scale_y_reverse()
plot of chunk example-ggplot2-scale_x_continuous-1
# Or you can supply a transformation in the `trans` argument:
p1 + scale_y_continuous(trans = scales::reciprocal_trans())
plot of chunk example-ggplot2-scale_x_continuous-1
# You can also create your own. See ?scales::trans_new

[Package ggplot2 version 3.3.6 Index]