The scale_x_discrete Function in R

Use
scale_x_discrete
to Reverse Order of Items onx
Axis in R Plot 
Use
scale_x_discrete
to Display Subset of Items onx
Axis in R 
Use
scale_x_discrete
to Rename Item Labels onx
Axis in R 
Use
scale_x_discrete
to Modifyx
Axis Name in R
This article will introduce the scale_x_discrete
function in R.
Use scale_x_discrete
to Reverse Order of Items on x
Axis in R Plot
scale_x_discrete
is used to set values for discrete xaxis scale aesthetics. In this article, we construct multiple boxplot graphs using ggplot
and geom_boxplot
functions to demonstrate the effect of different parameters in scale_x_discrete
. It’s often the case that the order of items on the axis needs to be reversed, and the easiest method to accomplish this is to set the limits
parameter of the scale_x_discrete
parameter with the value of rev(levels(dataset_name$X_axis_items))
. Notice that the dataset name comes first, followed by the $
symbol, and then we specify the xaxis data that needs to be reversed.
library(ggplot2)
library(gridExtra)
p1 < ggplot(Loblolly, aes(x = Seed, y = height)) +
geom_boxplot(fill = "cyan")
p2 < ggplot(Loblolly, aes(x = Seed, y = height)) +
geom_boxplot(fill = "orange") +
scale_x_discrete(limits = rev(levels(Loblolly$Seed)))
grid.arrange(p1, p2, ncol = 2, nrow =2)
Use scale_x_discrete
to Display Subset of Items on x
Axis in R
Another useful feature of the scale_x_discrete
function is to eliminate some items from the xaxis and draw only a handful of them. In this case, we utilize the PlantGrowth
dataset where three categories of groups are listed. Thus, we can output the boxplot with only trt2
and trt1
groups, as shown in the following code snippet.
library(ggplot2)
library(gridExtra)
p1 < ggplot(Loblolly, aes(x = Seed, y = height)) +
geom_boxplot(fill = "cyan")
p2 < ggplot(Loblolly, aes(x = Seed, y = height)) +
geom_boxplot(fill = "orange") +
scale_x_discrete(limits = rev(levels(Loblolly$Seed)))
p3 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "pink")
p4 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "green") +
scale_x_discrete(limits = c("trt2", "trt1"))
grid.arrange(p1, p2, p3, p4, ncol = 2, nrow =2)
Use scale_x_discrete
to Rename Item Labels on x
Axis in R
The scale_x_discrete
function can also be used to rename item labels along the xaxis. New label values can be supplied with the vector assigned to the labels
parameter in the scale_x_discrete
function.
library(ggplot2)
library(gridExtra)
p1 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "cyan")
p2 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "pink") +
scale_x_discrete(
labels = c("Control", "Treatment 1", "Treatment 2")
)
grid.arrange(p1, p2, ncol = 2, nrow =2)
Use scale_x_discrete
to Modify x
Axis Name in R
Notice that each previous method can be combined to output desired graph structure. E.g., the following example code draws p4
to show only the subset of items and rename these labels with supplied values. Additionally, we utilize scale_x_discrete
to modify the name along the xaxis using the name
parameter.
library(ggplot2)
library(gridExtra)
p1 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "cyan")
p2 < ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot(fill = "pink") +
scale_x_discrete(
labels = c("Control", "Treatment 1", "Treatment 2")
)
p3 < ggplot(OrchardSprays, aes(x = treatment, y = decrease)) +
geom_boxplot(fill = "orange")
p4 < ggplot(OrchardSprays, aes(x = treatment, y = decrease)) +
geom_boxplot(fill = "green") +
scale_x_discrete(
limits = c("A", "B"),
labels = c("Alice", "Bob"),
name = "Treatment"
)
grid.arrange(p1, p2, p3, p4, ncol = 2, nrow =2)
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front/backend programming.
LinkedIn Facebook