The pch in R is “short for plot characters, symbols, or shapes we can use to make plots”. It is an argument used in various plotting functions, such as plot(), points(), and lines(), to specify the type of symbol or marker to be used in the plot.
In R, there are 26 built-in shapes available for use, and they can be identified by numbers ranging from 0 to 25(“+”, “.”, “;”, etc.).
The first 19 (0:18) numbers represent S-compatible vector symbols, and the remaining 7 (19:25) represent the R-specific vector symbols.
The different point symbols commonly used in R are shown below:
- pch = 0, square
- pch = 1, circle
- pch = 2, triangle point up
- pch = 3, plus
- pch = 4, cross
- pch = 5, diamond
- pch = 6, triangle point down
- pch = 7, square cross
- pch = 8, star
- pch = 9, diamond plus
- pch = 10, circle plus
- pch = 11, triangles up and down
- pch = 12, square plus
- pch = 13, circle cross
- pch = 14, square and triangle down
- pch = 15, filled square
- pch = 16, filled circle
- pch = 17, filled triangle point-up
- pch = 18, filled diamond
- pch = 19, solid circle
- pch = 20, bullet (smaller circle)
- pch = 21, filled circle blue
- pch = 22, filled square blue
- pch = 23, filled diamond blue
- pch = 24, filled triangle point-up blue
- pch = 25, filled triangle point down blue
You can see the pch numbers from the above section, and R will put the symbol according to that number.
The point symbol pch = 16 is a filled circle. Therefore, the pch = 16 generates the filled circle figure in the plot.
The point symbol pch = 19 is a solid circle. Therefore, pch 19 generates the solid circle figure in the plot.
Example 1: Use the pch = 10
Let’s use the equation y = x^3. It means we will define two vectors, x and y. The y is the cube of x.
x <- c(1, 2, 3, 4, 5, 6, 7) y <- c(1, 8, 27, 64, 125, 216, 343)
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)
The default character for the plot is a circle.
To change that circle symbol, you can pass the pch with a proper argument ranging 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)
Example 2: 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.
- col: It is used for color (you can use the color code or color name) for the points.
- 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.
- cex: It is the size of pch symbols.
- 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)
Example 3: Using a built-in dataset
R has many built-in datasets; we will use the iris dataset in this tutorial.
To get the specific part of the object, use the head() function.
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)
Example 4: Applying a legend to a 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) )