Legend label errors with glmnet plot in R -


i modified function post ( adding labels on curves in glmnet plot in r ) add legend plot follows:

library(glmnet) fit = glmnet(as.matrix(mtcars[-1]), mtcars[,1])  lbs_fun <- function(fit, ...) {         l <- length(fit$lambda)         x <- log(fit$lambda[l])         y <- fit$beta[, l]         labs <- names(y)         text(x, y, labels=labs, ...)         legend('topright', legend=labs, col=1:length(labs), lty=1) # <<< added me } plot(fit, xvar="lambda") lbs_fun(fit) 

enter image description here

however, getting mismatch between text labels on plot , in legend. variable 'am' incorrectly colored. error? help.

plot(fit, xvar="lambda") utilizes function matplot. default, matplot uses 6 colors , recycles them. have create legend accordingly:

lbs_fun <- function(fit, ...) {         l <- length(fit$lambda)         x <- log(fit$lambda[l])         y <- fit$beta[, l]         labs <- names(y)         text(x, y, labels=labs, ...)         legend('topright', legend=labs, col=1:6, lty=1) # 6 colors } 

enter image description here


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -