How to use shapes in ggplot plot In R Language
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
R
# create sample data frame xAxis <- rnorm (100) yAxis <- rnorm (100) + xAxis + 10 df <- data.frame (xAxis, yAxis) # load library ggplot2 library (ggplot2) # create plot # use shape argument to shape plot # points using pch value ggplot (df, aes (x=xAxis, y=yAxis))+ geom_point (shape = 8) |
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 |