Today, we will learn about symbols in R together. If you want to know what those ways are, the syntax, and how to use them, let’s follow this article to the end.
Why do we use symbols in R?
There are many ways for the graph to be more beautiful and the blog has more views … One of the ways doing this is to add symbols to the graph, and R provides a package or function to draw symbols quickly. Here we are going to learn about it together.
How to use symbols in R
Plot symbols with ‘pch’
The plotting character or call it a pch
, and it is used to modify the points’ symbols with pch
parameters that can be selected numbers from 1 to 25. Besides, we can set the size of the symbols with the cex
argument and the line width with the lwd
argument. See the code below.
x0 <- c(sapply(seq(5, 25, 5), function(i) rep(i, 5))) y0 <- rep(seq(25, 5, -5), 5) plot(x0, y0, pch = 1:20, cex = 2, yaxt = "n", ylim = c(2, 26), xaxt = "n", xlim = c(2, 26), lwd = 1:2) text(x0 - 1.2, y0 + 0.05, 1:20)
Output:

We can set the background with the bg
argument.
x0 <- c(sapply(seq(5, 25, 5), function(i) rep(i, 5))) y0 <- rep(seq(25, 5, -5), 5) plot(x0, y0, pch = 1:20, cex = 2, yaxt = "n", ylim = c(2, 26), xaxt = "n", xlim = c(2, 26), lwd = 1:2, bg = 1:25, col = rainbow(30), ann = FALSE) text(x0 - 1.2, y0 + 0.05, 1:20)
Output:

So, we can perform the iris dataset by the method above as follows:
# Set up color color <- c("#33CC33", "#0099FF", "#FF0000") color <- color[as.numeric(iris$Species)] # Set up shape shape = c(19, 18, 17) shape <- shape[as.numeric(iris$Species)] # Plot plot(x = iris$Sepal.Length, y = iris$Sepal.Width, lwd = 1, ces = 1, main = "Species of Iris", col = color, pch = shape, xlab = "Length", ylab = "Width", cex = 2) legend("topright", legend = levels(iris$Species), col = c("#33CC33", "#0099FF", "#FF0000"), pch = c(19, 18, 17))
Output:

Plot symbols with symbols function
We can use the symbols function to draw circles, squares, and stars …
First, we need to add a graphics package before running the program. If you don’t know, click here.
Syntax:
symbols(x, y = NULL, circles, squares, rectangles, stars, thermometers, ...)
Parameters:
- x, y: The x,y coordinate.
- circles, squares, rectangles, stars, thermometers: The shape you want to plot.
- …: Another argument, e.g.: xlab, ylab, bg, lwd, …
Code example:
# Set up a package if you don't have if(!require('graphics')) { install.packages('graphics') library('graphics') } require(stats); require(grDevices) x <- 1:8 y <- sort(8 * runif(8)) z <- runif(8) zz <- cbind(z, 2 * runif(8), runif(8)) with(trees, { option <- palette(rainbow(30, end = 1.0)) symbols(xlab = "",ylab = "",x = Height, y = Volume, circles = Girth/16, inches = FALSE, lwd = 1.5, bg = 1:30, fg = "white", main = "Symbols") palette(option) })
Output:

Summary
Congratulations! We already know how to use symbols in R. We hope this article helps you. If you have any questions, please comment below. We will answer as possible. Thank you for reading!
Maybe you are interested:
- R Sample Function: How To Use The Sample() Function In R
- Names in R: How To Use The Names() Function in R?
- Spread() Function In R: How To Use spread() In R

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript