Color Customization
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.
Syntax:
plot( x, y, pch, cex, bg, col, lwd )
Parameter:
- 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 pch symbol outline
Example: Adding color customization to pch symbols
R
# create sample data frame xAxis <- rnorm (100) yAxis <- rnorm (100) + xAxis + 10 # create plot # use pch function to shape plot points # using pch value plot (xAxis, yAxis, pch = 21, cex=3, col= "green" , bg= "yellow" , lwd=4) |
Output:
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
R
# load library tidyverse library (tidyverse) # create dataframe of 26 character to # visualize the 26 characters df <- data.frame (p= c (0:25)) %>% mutate (x = rep ( seq_len ( ceiling ( n ()/6)), each = 6, length.out = n ())) %>% group_by (x)%>% mutate (y=1: n ()) # create plot using ggplot function # shape parameter is used to shape points ggplot (df, aes (x = x, y = y, shape = p),) + scale_shape_identity () + geom_point ( size = 6, fill = "green" )+ # geom_text function is used to label data points # with pch value geom_text ( aes (x = x - 0.3, y = y, label = paste0 ( "pch =" ,p)), size = 3) |
Output:
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 |
Contact Us