11  Modify Axis

In this chapter, we will learn how to modify the X and Y axis using the

NoteWhy this chart

Axis scales, breaks and labels decide what readers can read off the chart.

WarningCommon pitfall

Fighting default breaks with manual limits can clip data; prefer coord_cartesian() to zoom without dropping.

following functions:

11.1 How to modify a continuous axis in ggplot2

If the X and Y axis represent continuous data, we can use scale_x_continuous() and scale_y_continuous() to modify the axis. They take the following arguments:

  • name
  • limits
  • breaks
  • labels
  • position

Let us continue with the scatter plot we have used in previous chapter.

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

The name argument is used to modify the X axis label. In the below example, we change the X axis label to 'Displacement'. In previous chapters, we have used xlab() to work with the X axis label.

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

To modify the range, use the limits argument. It takes a vector of length 2 i.e. 2 values, the lower and upper limit of the range. It is an alternative for xlim().

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

In the above plot, the ticks on the X axis appear at 0, 200, 400 and 600. Let us say we want the ticks to appear more closer i.e. the difference between the tick should be reduced by 50. The breaks argument will allow us to specify where the ticks appear. It takes a numeric vector equal to the length of the number of ticks.

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

We can change the tick labels using the labels argument. In the below example, we use words instead of numbers. When adding labels, we need to ensure that the length of the breaks and labels are same.

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  scale_x_continuous(breaks = c(150, 300, 450),
    labels = c('One Hundred Fifty', 'Three Hundred', 'Four Hundred Fifty'))

The position of the axes can be changed using the position argument. In the below example, we can move the axes to the top of the plot by supplying the value 'top'.

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

11.1.1 How to combine continuous axis options in ggplot2

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

11.1.2 How to modify the y-axis in ggplot2

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

11.2 How to modify a discrete axis in ggplot2

If the X and Y axis represent discrete or categorical data, scale_x_discrete() and scale_y_discrete() can be used to modify them. They take the following arguments:

  • name
  • labels
  • breaks
  • position

The above options serve the same purpose as in the case of continuous scales.

11.2.1 How to change axis labels in ggplot2

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

11.2.2 How to change axis tick labels in ggplot2

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

11.2.3 How to change axis breaks in ggplot2

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

11.2.4 How to move the axis position in ggplot2

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

11.3 How to combine discrete axis options in ggplot2

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

11.4 Exercises

  1. Set custom breaks on the x axis with scale_x_continuous().
  2. Zoom to mpg > 20 with coord_cartesian() without dropping data.
  3. Explain when coord_cartesian() beats limits for zooming.

Worked solutions in solutions/axis.md (attempt first).

Where to go next

  • ggplot2 book (ggplot2-book.org) for theory behind this layer
  • Claus Wilke, Fundamentals of Data Visualization, for perception guidance