自定义S4类的R绘图方法

问题描述

我正在开发R包,并想为我的自定义S4类创建一个绘图函数。它应该像这样工作:

setClass("Person",representation(name = "character"))
me <- new("Person",name = "Dan")

plot(me) # should do something,say:

plot.new()
text(x = 0.5,y = 0.5,me@name)

解决方法

我知道了。 definition参数必须是采用参数x的函数。

setMethod("plot","Person",function(x) {
    plot.new()
    text(x = 0.5,y = 0.5,x@name)
})
plot(me)