-2

I am trying to write a plot method for a object of S3 class shwehart. This plot should show values of Xi, should include warning limits and actions limits lines. I tried to use plot.shewhart(x,y,...) and R gave me a plot, but without straight lines. Could anyone help me? Thanks in advance.

aa<-c(1:10)
bb<-c(127,128,128.5,125.5,129,126.5,129,126,125,130)
samean<-data.frame(aa,bb)

class(samean)<-"shewhart"


plot.shewhart<-function(x){
  mu=127
  sigma=3.4
  plot(x<-samean[,1],y<-samean[,2], type="b", col="blue", 
       xlim<-c(0,10), ylim<-c(120,140)   )

  abline(h=mu, col="blue")
  abline(h=mu+3*sigma/sqrt(100), col="green")
  abline(h=mu-3*sigma/sqrt(100), col="green")
}

plot(samean)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Amy_777
  • 115
  • 3
  • 14
  • What does "did not work" mean exactly. Where is your attempt to write the function? You should provide a more complete [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can actually run the code. – MrFlick Nov 10 '17 at 16:12
  • Please review [mcve]. – G. Grothendieck Nov 10 '17 at 16:19
  • If I help with this question, will you delete it like so many others? Or will you keep it up? I don't like helping on questions that just get deleted by the asker. – Gregor Thomas Nov 10 '17 at 16:22
  • I am really sorry for my last question's improper behaviors. I won't delete any questions I post in the future. This question I did not just use my code, but changed as a minimal, complete and verifiable example. And thank you guys very much for guiding my question post. – Amy_777 Nov 10 '17 at 16:29

1 Answers1

2

You have two problems in the code you post.

1) You want to use samean like a data frame--you want things like samean[, 1] to work--but also like your new class shewhart. So you don't want to replace the data.frame class, you want to extend it. We change your line

# class(samean)<-"shewhart" # bad
class(samean) <- c("shewhart", class(samean)) # good

This way it is still a data.frame, it's just a special type of data frame:

class(samean)
# [1] "shewhart"   "data.frame"

2) For function arguments, use = not <-. This is a problem in your plot command. Also, your function names its input x, so you should refer to it only as x inside the function.

# plot(x<-samean[,1],y<-samean[,2], type="b", col="blue", 
   xlim<-c(0,10), ylim<-c(120,140)   ) # bad

plot(x = x[,1], y = x[,2], type = "b", col = "blue", 
   xlim = c(0,10), ylim = c(120,140)   ) # good

With these changes, the code works as you describe. I re-formatted for readability, but the only real changes are the ones mentioned above.

aa <- c(1:10)
bb <- c(127, 128, 128.5, 125.5, 129, 126.5, 129, 126, 125, 130)
samean <- data.frame(aa, bb)

class(samean) <- c("shewhart", class(samean))

plot.shewhart <- function(x) {
  mu = 127
  sigma = 3.4
  plot(
    x = x[, 1],
    y = x[, 2],
    type = "b",
    col = "blue",
    xlim = c(0, 10),
    ylim = c(120, 140)
  )

  abline(h = mu, col = "blue")
  abline(h = mu + 3 * sigma / sqrt(100), col = "green")
  abline(h = mu - 3 * sigma / sqrt(100), col = "green")
}

plot(samean)

enter image description here

Please do not delete your question now that it has been answered.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • If you want to use this for your own functions, you have to first register the function as a Generic using the function `UseMethod`. `plot` is already a generic function. – MrGumble Nov 10 '17 at 16:40