Agenda


Modify X and Y axis

  • title
  • labels
  • limits
  • breaks
  • position

Intro


In this module, we will learn how to modify the X and Y axis using the following functions:

  • Continuous Axis
    • scale_x_continuous()
    • scale_y_continuous()
  • Discrete Axis
    • scale_x_discrete()
    • scale_y_discrete()

Library


library(ggplot2)
library(dplyr)
library(tidyr)

Continuous Axis


scale_x_continuous() and scale_y_continuous() take the following arguments:

  • name
  • limits
  • breaks
  • labels
  • position

X Axis - Continuous


ggplot(mtcars) +
  geom_point(aes(disp, mpg))

Axis Label


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(name = "Displacement")

Axis Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(limits = c(0, 600))

Axis Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(breaks = c(0, 150, 300, 450, 600))

Axis Tick Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(breaks = c(0, 150, 300, 450, 600),
    labels = c('0', '150', '300', '450', '600'))

Axis Position


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(position = 'top')

X Axis


ggplot(mtcars) + geom_point(aes(disp, mpg)) +
  scale_x_continuous(name = "Displacement", limits = c(0, 600),
                     breaks = c(0, 150, 300, 450, 600), position = 'top',
                     labels = c('0', '150', '300', '450', '600'))

Y Axis - Continuous


ggplot(mtcars) + geom_point(aes(disp, mpg)) +
  scale_y_continuous(name = "Miles Per Gallon", limits = c(0, 45),
                     breaks = c(0, 15, 30, 45), position = 'right',
                     labels = c('0', '15', '30', '45'))

Discrete Axis


scale_x_discrete() and scale_y_discrete() take the following arguments:

  • name
  • labels
  • breaks
  • position

Axis Label


ggplot(mtcars) +
  geom_bar(aes(factor(cyl))) +
  scale_x_discrete(name = "Number of Cylinders")

Axis Tick Labels


ggplot(mtcars) +
  geom_bar(aes(factor(cyl))) +
  scale_x_discrete(labels = c("4" = "Four", "6" = "Six", "8" = "Eight"))

Axis Breaks


ggplot(mtcars) +
  geom_bar(aes(factor(cyl))) +
  scale_x_discrete(breaks = c("4", "6", "8"))

Axis Position


ggplot(mtcars) +
  geom_bar(aes(factor(cyl))) +
  scale_x_discrete(position = 'bottom')

X Axis - Discrete

ggplot(mtcars) + geom_bar(aes(factor(cyl))) +
  scale_x_discrete(name = "Number of Cylinders",
    labels = c("4" = "Four", "6" = "Six", "8" = "Eight"),
    breaks = c("4", "6", "8"), position = "bottom")