-1

I am trying regression with modified iris dataset using msgps package in R:

iris2 = iris
iris2$Species = as.numeric(iris2$Species)
str(iris2)
    'data.frame':   150 obs. of  5 variables:
     $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
     $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
     $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
     $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
     $ Species     : num  1 1 1 1 1 1 1 1 1 1 ...

library(msgps)
fit = msgps(as.matrix(iris2[1:4]), iris2$Species)
plot(fit, criteria='cp')
legend('topleft', names(iris2[1:4]), lty=1:4, col=1:4) 

enter image description here

The legend is clearly wrong. It has a dashed red line which is not there in the main graph. The main graph has a light blue line which is not there in the legend. Where is the problem? Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • 2
    The legend plotted exactly what you asked for. You need to specify the colors and linetypes used in the plot method for msgps. Not sure what connection you thought `1:4` would have to the colors/linetypes used in a previous function call. – joran Jun 22 '15 at 16:58
  • I presumed that msgps function is using predictor names in the same order as they exist in the matrix. On plotting different colors and linetypes, it appears msgps function is using lty=c(1,3:5), col=c(1,3:5). I do not understand why it is doing this. – rnso Jun 22 '15 at 17:14
  • I wouldn't assume anything about how `plot.msgps` is choosing to assign colors or line types without first looking at the code in that function. As for why.... Because it made sense to the package author? – joran Jun 22 '15 at 17:20

1 Answers1

0

I'm not certain but my guess would be that your code legend(... lty=1:4, col=1:4) only changes the colors and line types in the legend, and not the plot itself. I would try putting that code in the plot() function as well. This is to ensure R knows that the col and lty values are synced in the plot function and the legend function.

Buzz Lightyear
  • 824
  • 1
  • 7
  • 18
ila
  • 709
  • 4
  • 15
  • 1
    Though I did not downvote, I also think the answer would be better if actual working code is shown. Otherwise, this is more like a comment. The main plot() function comes with the package and is much more complex. – rnso Jun 22 '15 at 18:21