pch in R: How to Use Plot Character in R

While creating a plot in R, you can add explanatory text like axis labels, titles, legends, or text. In addition, there are many plotting symbols available in R that can be used in plots. For example, the graphical argument used to specify point shapes is pch, and we will discuss the same in this article.

pch in R

The pch in R defines the point symbols in the functions plot() and lines(). The pch stands for plot character.  The pch contains numeric values rangers from 0 to 25 or character symbols (“+”, “.”, “;”, etc.) specifying in symbols (or shapes).

The pch is the standard argument to set the character plotted in several R functions.

The different points symbols commonly used in R are shown below.

plot character in R

The numerical values according to its symbols are the following.

  1. pch = 0, square
  2. pch = 1, circle
  3. pch = 2, triangle point up
  4. pch = 3, plus
  5. pch = 4, cross
  6. pch = 5, diamond
  7. pch = 6, triangle point down
  8. pch = 7, square cross
  9. pch = 8, star
  10. pch = 9, diamond plus
  11. pch = 10, circle plus
  12. pch = 11, triangles up and down
  13. pch = 12, square plus
  14. pch = 13, circle cross
  15. pch = 14, square and triangle down
  16. pch = 15, filled square
  17. pch = 16, filled circle
  18. pch = 17, filled triangle point-up
  19. pch = 18, filled diamond
  20. pch = 19, solid circle
  21. pch = 20,bullet (smaller circle)
  22. pch = 21, filled circle blue
  23. pch = 22, filled square blue
  24. pch = 23, filled diamond blue
  25. pch = 24, filled triangle point-up blue
  26. pch = 25, filled triangle point down blue

Example

Let’s use the equation y = x^3Let’ss means we will define two vectors, x, y, and y is the cube of x.

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

Now, let’s plot the y = x^3 values.

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

plot(x, y)

Output

pch in R

The default character for the plot is a circle.

If you want to change that circle symbol, then you can pass the pch with a proper argument that ranges from 0 to 26.

Let’s pass pch = 10.

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

plot(x, y, pch=10)

Output

pch = 10 in R

You can see that the symbol is changed, and now it is diamond plus.

Change background color and size of pch

One of the most prominent features of the pch codes is that you can modify their background fill color and their borderline type, color, size of the symbol, and line width of the plotting symbols.

  1. col: It is used for color (you can use the color code or color name) to use for the points.
  2. bg: It is used in the background (or fill) color for the open plot symbols. It can be used only when pch ranges from 21 to 25, including 21 and 25.
  3. cex: It is the size of pch symbols.
  4. lwd: It is the line width for the plotting symbols.

Let’s pass all these parameters to the plot() function and see the output.

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

plot(x, y, pch=22, col = "darkgreen", bg = "yellow", cex = 1.9, lwd=2)

Output

Change background color and size of pch

We have defined the color of the text using the col argument, and we can change the text size with the cex argument. For example, by specifying cex = 1.9, we have specified the text to be 90% larger than normal.

The Numeric values indicate the line width of the border of the plotting symbol.

Using built-in R dataset

R installed with many inbuilt datasets, and in this tutorial, we will use the iris dataset.

To get the specific part of the object in R, use the head() function. But, first, let’s use the head() function. Then, let’s find out its content.

head(iris)

Output

head function in r

Let’s use the plot() function Let’seate a graph based on the Sepal.Length and Sepal.Width.

We will create two plots and add properties like cex, xlab, ylab, lwd, pch, and col.

par(mfrow = c(1, 2))

plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
 xlab = "Sepal Length", ylab = "Sepal Width",
 pch = 24, col = "darkgreen", bg = "yellow", 
 cex = 1, lwd = 1.3, frame = FALSE)

# Change plot symbol to pch = 15 (filled square)
plot(x = iris$Sepal.Length, y = iris$Sepal.Width,
 xlab = "Sepal Length", ylab = "Sepal Width",
 pch = 15, col = "red", 
 cex = 1.5, lwd=1.3, frame = FALSE)

Output

plot iris dataset in R

Applying a legend to a plot

To create a legend in the plot, use the legend() function.

Let’s create a legend in the tLet’sght of the iris dataset plot.

# Colors
colors <- c("red", "darkgreen", "blue")
colors <- colors[as.numeric(iris$Species)]

# Shapes
shapes = c(22, 23, 24) 
shapes <- shapes[as.numeric(iris$Species)]

# Plot
plot(x = iris$Sepal.Length, y = iris$Sepal.Width, frame = FALSE,
 xlab = "Sepal Length", ylab = "Sepal Width",
 col = colors, pch = shapes)

# Legend
legend("topright", legend = levels(iris$Species),
 col = c("red", "darkgreen", "blue"),
 pch = c(22, 23, 24) )

Output

pch in R with example from scratch

Conclusion

In this article, we have seen what pch is and the different types of plot characters that we can use while developing the plot. We have then seen how to modify the pch using different properties like cex, bg, color, lwd, and other valid explanatory data such as legend xlab, ylab, etc.,

That is it for this tutorial.

Leave a Comment