Guides and Axes

Introduction

Agenda


Modify X and Y axis

  • title
  • labels
  • limits
  • breaks
  • position

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()

Plot


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

Axis Label

Continuous


Example


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

Instructions


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

Discrete


Example


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

Instructions


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

Axis Limits

Continuous


Example


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

Instructions


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

Axis Breaks

Continuous


Example


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

Instructions


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

Discrete


Example


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

Instructions


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

Axis Tick Labels

Continuous


Example


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

Instructions


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

Discrete


Example


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

Instructions


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

Axis Position

Continuous


Example


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

Instructions


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

Discrete


Example


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

Instructions


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

X Axis - Summary

X Axis - Continuous


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'))

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")

Y Axis - Summary


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'))