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.
The numerical values according to its symbols are the following.
- 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
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)
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)
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) to use 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 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.
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.