While creating a plot in R, you can add explanatory text like axis labels, titles, legends, or text. In addition, many plotting symbols available in R 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 post.
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 ranging 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 point symbols commonly used in R are shown below.
pch symbols in R
- 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.
pch=16 in r
The point symbol pch = 16 in R is a filled circle. The pch = 16 generates the filled circle figure in the plot.
pch 19 in R
The point symbol pch = 19 in R is a solid circle. The pch 19 generates the solid circle figure in the plot.
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)
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)
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)
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.
- 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)
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 is installed with many built-in 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.
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)
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) )
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.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.