pch in R

This tutorial demonstrates the use of pch in R.

pch in R

The pch abbreviated as plot character is the standard argument used to set the character, which will be plotted in different R functions. The pch also can be defined as the explanatory symbol added to a plot; this explanatory text includes legends, axis labels, or titles.

The pch are the symbols you want to add to a plot that explains a certain thing. Here is the list of pch symbols with their values.

pch = 0 square
pch = 1 circle
pch = 2 point up triangle
pch = 3 plus
pch = 4 cross
pch = 5 diamond
pch = 6 point-down triangle
pch = 7 square cross
pch = 8 star
pch = 9 diamond plus
pch = 10 circle plus
pch = 11 up and down triangles
pch = 12 square plus
pch = 13 circle cross
pch = 14 square and down triangle
pch = 15 filled square
pch = 16 filled circle
pch = 17 point-up filled triangle
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

The pch is passed as an argument to the legend; the pch method will include the value from the above list. Let’s try an example for the pch.

#data
set.seed(1234)
delftstack <- data.frame(x=runif(10),y=runif(10),
                 color=rep(c("lightblue","red"),5),
                 background=rep(c("red", "lightblue"),5),
                 shape=rep(c(21,24),5),stringsAsFactors = FALSE)

#plot
plot(delftstack$x,delftstack$y,col=delftstack$color,bg=delftstack$background,pch=delftstack$shape,cex=2)
points(x=0.5,y=0.5,pch=8,col="red",cex=2)

#add a legend, pt.bg is needed to fill in the background of the shape
legend(0.01,0.5,
       legend = c(expression("PCH"["Example1"]),
                  expression("PCH"["Example2"]),
                  expression("PCH"["Example"])),
       pch = c(21,24,8),
       col = c("lightblue","red","red"),
       pt.bg = c("red","lightblue"),
       cex=2)

The code above plots the data from the data frame, and then in legend, it tries to apply some characteristics, which also includes pch. See the output:

PCH in R