Customizing ggplot2 axis and labels - how to make them more informative

What's the most important thing about graphs with coordinates when visualizing data? It's all about adjusting the axes and labels to make the information easier to understand. Using R's ggplot2 packageprovides a wide range of axis customization options that meet this need well.

Today we'll be using Use the ggplot2 axis option to freely set the axes and labels of your graphsand we'll explain it in detail with each piece of code to make it easy for beginners to follow along!

By default, the ggplot2automatically sets the ranges and labels for each axis. However, there are many times when you need to manually adjust them to fit your data. Today we'll learn how to adjust the x- and y-axes, as well as how to set the ranges manually.

OptionsDescription.Key parameters
scale_x_continuous() / scale_y_continuous()Adjust the range, spacing, and labels of continuous axes.limits, breaks, labels
scale_x_discrete() / scale_y_discrete()Set axis properties for categorical data.labels, expand, limits
coord_flip()Flip the x- and y-axes to show them vertically.None (flip axis)
labs()Add titles and labels for the x- and y-axis.title, x, y
theme(axis.text.x)Adjust the angle, color, size, and more of the x-axis text.angle, hjust, color, size
theme(axis.text.y)Adjust the rotation, color, font size, and more of the y-axis text.angle, vjust, color, size

1. draw a basic line graph with geom_line()

ggplot2 axis - line graph

First, let's plot a line graph using geom_line(). Running the code below will create a graph like the one shown above.

Load the # library
library(ggplot2)

Generate # example data
data <- data.frame(
  x = 1:10,
  y = c(3, 5, 8, 4, 7, 9, 10, 6, 8, 7)
)

Generate a # base line graph
ggplot(data, aes(x = x, y = y)) +
  geom_line() +
  labs(title = "Basic line graph", x = "x-axis", y = "y-axis")

Code description:

  • library(ggplot2): Load the ggplot2 package.
  • aes(x = x, y = y): Specifies the data to use for the x and y axes.
  • geom_line(): Generate a line graph.
  • labs(): Adds a title and axis labels to the graph.

2. Customize axis labels: scale_x_continuous() and scale_y_continuous()

ggplot2 axis - line graph2

scale_x_continuous() and scale_y_continuous() are useful for scaling the axes of continuous data. You can adjust the range of the axes or relabel them. Running the code below will create a graph like the one shown above.

Load the # library
library(ggplot2)

Generate # example data
data <- data.frame(
  x = 1:10,
  y = c(3, 5, 8, 4, 7, 9, 10, 6, 8, 7)
)

Customize the # axis range and labels
ggplot(data, aes(x = x, y = y)) +
  geom_line()
  scale_x_continuous(limits = c(0, 12), breaks = seq(0, 12, 2), labels = paste0(seq(0, 12, 2), "units")) +
  scale_y_continuous(limits = c(0, 12), breaks = seq(0, 12, 2)) +
  labs(title = "Customized axes", x = "X axis", y = "Y axis")

Code description:

  • scale_x_continuous(limits = c(0, 12), breaks = seq(0, 12, 2), labels = paste0(seq(0, 12, 2), "units")): Limits the range of the x-axis from 0 to 12, sets labels to 2 units, and adds "units" to each label.
  • scale_y_continuous(limits = c(0, 12), breaks = seq(0, 12, 2)): Limits the range of the y-axis from 0 to 12, and sets the label to 2 units.

3. Rotate and reposition axes: using the theme() function

ggplot2 axis - line graph3

Rotating or repositioning the axis labels can make interpreting the data much easier. Let's use the theme() function to adjust the label angles. Running the code below will create a graph like the one shown above.

Load the # library
library(ggplot2)

Generate # example data
data <- data.frame(
  x = 1:10,
  y = c(3, 5, 8, 4, 7, 9, 10, 6, 8, 7)
)

Example of rotating labels on the # axis
ggplot(data, aes(x = x, y = y)) +
  geom_line()
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Rotate X-axis labels", x = "X axis", y = "Y axis")

Code description:

  • theme(axis.text.x = element_text(angle = 45, hjust = 1)): Rotate the x-axis label by 45 degrees, and set hjust to 1 to align the label to the right.

4. Convert the coordinate system: flip the x and y axes with coord_flip()

ggplot2 axis - line graph4

Sometimes it's better to flip the x-axis and y-axis to get a better view. coord_flip() makes it easy to swap the x-axis and y-axis. Running the code below will create a graph like the one shown above.

Load the # library
library(ggplot2)

Generate # example data
data <- data.frame(
  x = 1:10,
  y = c(3, 5, 8, 4, 7, 9, 10, 6, 8, 7)
)

Example # coordinate system transformation
ggplot(data, aes(x = x, y = y)) +
  geom_line() +
  coord_flip() +
  labs(title = "Flip X, Y axis", x = "X axis", y = "Y axis")

Code description:

  • coord_flip(): Flips the x- and y-axes to represent the data vertically. This option is useful when you want to improve readability.

Wrap-up: Understand your data better with the ggplot2 axis option!

We've seen how the ggplot2 axis options can help you communicate your data more clearly. Small changes like adjusting the axis range, customizing labels, rotating, and converting coordinate systems can make your data more interpretable.

Now go ahead and experiment with ggplot2's axis options on your own! In the next post, we'll look at the How to make your data visualizations come alive with ggplot2 color optionsI'll do it.

Similar Posts