plot() in R

plot() is one of the generic functions that implement polymorphism in R. It produces a different graph if it is given a vector, a factor, a data frame, etc. But have you ever wondered how does the class of vectors or factors determine the method used for plotting?  Let’s see the code for the plot function. 

Example: Code of plot function

R




plot


Output:

function (x, y, ...)
UseMethod("plot")

We can see that the body of the plot function contains only one expression and that is UseMethod(“plot”). Let’s see the definition of UseMethod with the help of help() function.

Example: Definition of the help() method.

R




help(UseMethod)


Output:

From the above output, we can see that UseMethod takes two parameters generic and object. 

  • The generic is the string name which is the name of the function (plot in this case). 
  • This is an object whose class will determine the method that will be “dispatched,” It means the object for which the generic method will be called.

The UseMethod then searches for the suitable plot function that is needed to be called by creating a string of the type plot.object. We can also see all the available methods for the plot function.

Example:

R




methods(plot)


Output:

Let’s see how plot() function taking arguments and displaying different outputs

Input is one numeric vector

In this example let’s take a single numeric vector inside plot() function as a parameter.

R




# R program to illustrate
# polymorphosim
 
# X Window System Graphics (X11)
X11(width = 15, height = 13)
 
# The runif() function generates
# random deviates of the uniform distribution
x <- 3 * runif(40) + (1:30)
par(mar = c(20, 20, 1, 1))
 
# type='l' is used  to connect the points
# of the scatter plots with lines.
plot(x, type = 'l', col = '#343deb')
 
# We can do  mouse click or enter pressed
z <- locator(1)


 Output:

Inputs are two numeric vectors

We need to pass two vector parameters and it produces a scatter plot accordingly.

R




# R program to illustrate
# polymorphosim
 
# X Window System Graphics (X11)
X11(width = 5, height = 3)
 
# The runif() function generates random
# deviates of the uniform distribution
x <- runif(20)
y <- runif(20) * x
par(mar = c(2, 2, 0.3, 0.3))
 
# type = 'p' means as points, the output comes as scattered
# pch stands for plot character. pch = 16 we get . character
plot(x, y, type = 'p', pch = 16, col = '#ab1ab0')
 
#Either mouse press or enter key press wait
z <- locator(1)


 Output:

Input is a factor

If we passed factor as arguments then we get a bar chart pattern. 

R




# R program to illustrate
# polymorphosim
 
# X Window System Graphics (X11)
X11(width = 5, height = 8)
 
# here fruits names are passed and barchart is produced as output
f <- factor(c('apple', 'orange', 'apple', 'pear', 'orange',
              'apple', 'apple', 'orange'))
par(mar = c(2, 2, 0.6, 0.6))
 
# Using plot()
plot(f, col = '#8f4c91')
z <- locator(1)


 Output:

Input is a data frame

The Plot function takes the data frame as an argument and each variable of the data frame is plotted against each other.

R




# R program to illustrate
# polymorphosim
 
# X Window System Graphics (X11)
X11(width = 6, height = 6)
 
set.seed(280870)
x <- c(4, 3, 1, 2, 2, 4, 6, 4, 5, 5,
       4, 4, 5, 4, 4, 8, 4, 1, 2, 7)
y <- x * rnorm(20, 1, 0.3)
z <- x * y
 
# Taking a data frame
df <- data.frame(x, y, z)
par(mar = c(0.1, 0.1, 0.1, 0.1))
 
# Using plot()
plot(df, col = '#a832a6', pch = 16, cex = 1.5)
z <- locator(1)


 Output:

Polymorphism in R Programming

R language is evolving and it implements parametric polymorphism, which means that methods in R refer to functions, not classes. Parametric polymorphism primarily lets you define a generic method or function for types of objects you haven’t yet defined and may never do. This means that one can use the same name for several functions with different sets of arguments and from various classes. R’s method call mechanism is generics which allows registering certain names to be treated as methods in R, and they act as dispatchers. 

Similar Reads

Generic Functions

Polymorphism in R can be obtained by the generics. It allows certain names to be treated as methods and they act as dispatchers. Let’s understand with the help of plot() function and summary function. In R programming the plot() and summary() functions return different results depending on the objects being passed to them and that’s why they are generic functions that implement polymorphism....

plot() in R

plot() is one of the generic functions that implement polymorphism in R. It produces a different graph if it is given a vector, a factor, a data frame, etc. But have you ever wondered how does the class of vectors or factors determine the method used for plotting?  Let’s see the code for the plot function....

summary() in R

...

Creating Generic Method

...

Contact Us