How to Use scale_x_discrete in R


You can use the scale_x_discrete() function in ggplot2 to specify the values to use along a discrete x-axis in a plot.

This function uses the following basic syntax:

p +
  scale_x_discrete(name, labels, limits, ...)

where:

  • name: Name of the variable to use on x-axis
  • labels: A vector of labels to use for the x-axis
  • limits: A vector that specifies the min and max value for the x-axis

The labels argument is particularly useful for specifying the exact labels that should be used along the x-axis to represent factors or categorical variables.

The following example shows how to use the scale_x_discrete() function in practice in R.

Example: How to Use scale_x_discrete in R

For this example we will create the following dataset that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'C', 'C', 'C', 'C'),
                 points=c(12, 15, 22, 24, 20, 40, 12, 18, 11),
                 assists=c(4, 6, 6, 2, 8, 0, 1, 8, 13))

#view data frame
df

  team points assists
1    A     12       4
2    A     15       6
3    A     22       6
4    B     24       2
5    B     20       8
6    C     40       0
7    C     12       1
8    C     18       8
9    C     11      13

The dataset contains information about the team name, points scored and total assists for 9 different basketball players.

Suppose that we would like to create a bar plot to visualize the total count of players by team.

We can use the geom_bar() function from ggplot2 to do so.

If you don’t have the ggplot2 data visualization package already installed then you can use the following syntax to do so:

#install ggplot2 package if necessary
install.packages('ggplot2')

Once the package is installed, you can use the following syntax to create a bar chart:

library(ggplot2)

#create bar plot to visualize count by team
ggplot(df, aes(team)) +
  geom_bar()

This produces the following plot:

The x-axis displays each of the unique team names and the y-axis displays the total count of players on each team.

We can see that the x-axis currently uses the names of the teams for the axis labels: A, B and C.

Suppose that we would like to customize the x-axis labels to instead display different names.

The easiest way to do so is by using the labels argument within the scale_x_discrete() function.

For example, we can use the following syntax to use the following x-axis labels instead:

  • Team A
  • Team B
  • Team C
library(ggplot2)

#create bar plot to visualize count by team
ggplot(df, aes(team)) +
  geom_bar() +
  scale_x_discrete(labels=c('Team A', 'Team B', 'Team C'))

This produces the following plot:

Notice that the x-axis now has the exact labels that we specified within the labels argument.

You can also easily change the x-axis title by specifying the title first in the scale_x_discrete function.

For example, we can use the following syntax to specify both the x-axis label and the x-axis title:

library(ggplot2)

#create bar plot to visualize count by team
ggplot(df, aes(team)) +
  geom_bar() +
  scale_x_discrete('Team Names', labels=c('Team A', 'Team B', 'Team C'))

This produces the following plot:

Example of scale_x_discrete in R

Note: You can find the complete documentation for the scale_x_discrete function in ggplot2 here.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Use scale_y_continuous in ggplot2
How to Rotate Axis Labels in ggplot2
How to Change Legend Labels in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *