R plot pch symbols – Different point shapes available in R
In this article, we will discuss Pch that are built-in shapes in the R programming language.
The pch (plot characters) in the R programming language are symbols or shapes we use for making plots. In the R Language, there are 26 built-in shapes that can be used in any graphic work in R Language and can be identified by numbers ranging from 0 to 25. The first 19 symbols are also known as s-compatible vector symbols and the remaining 7 are called the R-specific vector symbols. Characters other than numbers can also be used to specify pch including “+”, “*“,”-“,”.“,”#, “%”, “o”.
When we make a basic scatter plot, by default, it uses a dot to represent data that is pch=16. But we can convert it to any other shape using the desired pch value. Since remembering the pch value for each shape is difficult we can create a plot like the following to get the values of each symbol.
Example: Displaying all pch symbol
Following are the Pch value for different shapes.
value shape 0 square 1 circle 2 triangle point up 3 plus 4 cross 5 diamond 6 triangle point down 7 square cross 8 star 9 diamond plus 10 circle plus 11 triangles up and down 12 square plus 13 circle cross 14 square and triangle down 15 filled square 16 filled circle 17 filled triangle point-up 18 filled diamond 19 solid circle 20 bullet (smaller circle) 21 filled circle blue 22 filled square blue 23 filled diamond blue 24 filled triangle point-up blue 25 filled triangle point down blue
Using special characters as Pch values
Some special characters can also be used as values of the pch symbol instead of the numbers. In the R Language, we can use “+”, “*“,”-“,”.“,”#, “%”, and “o” for plotting different symbols. These symbols convert the pch shape exactly to their own shape. Below is a plot showing these symbols in action.
Example: Using special symbols as Pch values
Using shapes in ggplot plot
Now we can plot, scatter plot using the desired shape by putting the pch number at a shape value. We can use these shapes at any place in the R Language where shape vectors are used.
Example: A basic scatter plot with star shape using pch value as 8
Omitting points from the plot
To omit or remove points from any plot, we can use the pch value of NA. This makes the shape of points in a plot as zero-sized or invisible that giving an impression of removal of points.
plot( x, y , pch= NA )
Example: Removing all points
We can change the color of the outline, background fill, size of the symbol, and line width of the outline by using col, bg, cex, and lwd parameters respectively.
plot( x, y, pch, cex, bg, col, lwd )
- col : determines the color for the outline of the shape
- bg : determines the color for the background fill of the shape
- cex : determines the size of the pch symbol
- lwd : determines the line width for the the pch symbol outline
Example: Adding color customization to pch symbols