bty in R: Control box type in R Plotting

To conduct an impactful message to the viewer, you can change the look and feel of plots in R using R’s numerous plot options. We have already seen the lwd and pch options in the plot() function. In today’s article, we will see how to use the bty option to change the charts’ box type.

bty in R

The bty option in the R plot() function controls the box style of base. The bty option of the par() method allows the custom of the box around the plot. The shape of the letter represents the boundaries.

  1. o: complete box (default parameter),
  2. n: no box
  3. 7: top + right
  4. L: bottom + left
  5. C: top + left + bottom
  6. U: left + bottom + right

Let’s create a simple plot and create different types of boxes using the bty option.

par(mfrow = c(2, 3))

# Create data
x <- c(1, 2, 3, 4, 5, 6, 7)
y <- c(1, 8, 27, 64, 125, 216, 343)

# First graph
par(bty = "l")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "bottom left")

# Second graph
par(bty = "o")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "Complete box")

# Third graph
par(bty = "c")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "top + left + bottom")

# Fourth graph
par(bty = "n")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "no box")

# Fifth graph
par(bty = "U")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "left + bottom + right")

# Sixth graph
par(bty = "7")
plot(x, y, pch = 23, col = "black", bg = "yellow", lwd = 1, cex = 1.5, xlab = "top + right")

Output

Control box type in R Plotting

The bty option of the par() function allows customizing the box around the plot.

To plot a chart of a function in R, use the plot() function. The plot() is an inbuilt generic method for plotting R objects. The bty parameter determines the type of box drawn. Then we have plotted a graph using six different box types.

Using boxplot() function with bty argument

Boxplots are a measure of how well the data is distributed in the data set.

It divides the data set into three quartiles. This graph represents the minimum, maximum, median, first quartile and the third quartile in the data set.

par(mfrow = c(2, 3))

# Create data
a <- seq(1, 21) + 3 * runif(21, 0.3)
b <- seq(1, 21) ^ 2 + runif(21, 0.98)

# First graph
par(bty = "l")
boxplot(a, col = "purple", xlab = "bottom & left box")

# Second graph
par(bty = "o")
boxplot(b, col = "purple", xlab = "complete box", horizontal = TRUE)

# Third graph
par(bty = "c")
boxplot(a, col = "purple", xlab = "up & bottom & left box", width = 0.5)

# Fourth graph
par(bty = "n")
boxplot(a, col = "purple", xlab = "no box")

# Fifth graph
par(bty = "U")
boxplot(a, col = "purple", xlab = "left + bottom + right")

# Sixth graph
par(bty = "7")
boxplot(b, col = "purple", xlab = "top + right")

Output

bty in R

That is it for bty in R tutorial.

See also

Scatter plot in R

2 thoughts on “bty in R: Control box type in R Plotting”

Leave a Comment