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)
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 }
Comments
Post a Comment