Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which plot() is being used?

Tags:

plot

r

In R, you often see plot() being used with very different kinds of data with very different default outcomes. plot() recognizes which object it is been given and uses the proper function according to this object.

In the example below plot() actually refers to ape::plot.phylo().

library(ape)
tree.owls <- 
  read.tree(text = 
              "(((Strix_aluco:4.2,Asio_otus:4.2):3.1, Athene_noctua:7.3):6.3,Tyto_alba:13.5);")
plot(tree.owls)

Question is: how do you know that in this case plot() refers to plot.phylo()? More generally, ss there a way to find out that would apply to any object being plotted (vector, df, list, S3, S4, etc.)?

like image 759
Sebastien Renaut Avatar asked May 05 '26 07:05

Sebastien Renaut


1 Answers

As other's have mentioned, this has to do with S3 method dispatch. You can check what the class of an object is with the class function. In this case it returns phylo.

A lot of generic functions have many methods. You can check all the methods by using methods("plot").

The source code for the plot function is

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

and the UseMethod function will search for the available methods on the generic function given. If it can find the method, in this case plot.phylo then it will execute that method, otherwise it uses the next method or the default method.

So to reiterate, what method plot uses entirely depends on the class of the object, not so much if the object is a dataframe, vector, list, etc. Classes are convenient in guaranteeing a function will behave in an expected way.

To really make a point, you can define your own plot method too. Take this for example:

plot.foo <- function(x){
      str(x)
      plot(iris)
}

obj1 <- 1:3
class(obj1) <- "foo"
obj2 <- list(x = 2, y = 1:100)
class(obj2) <- "foo"

#No matter the object we pass, so long as the class if "foo",
# our custom plot method is called

plot(obj1)
#Class 'foo'  int [1:3] 1 2 3
plot(obj2)
#List of 2
 #$ x: num 2
 #$ y: int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
 # - attr(*, "class")= chr "foo"
like image 177
Justin Landis Avatar answered May 09 '26 06:05

Justin Landis