R中glmnet图的图例标签错误 [英] Legend label errors with glmnet plot in R
本文介绍了R中glmnet图的图例标签错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我修改了这篇文章中的函数(在曲线在 Glmnet 图中 R )添加图例,如下所示:
I modified the function from this post ( Adding labels on curves in glmnet plot in R ) to add legend to the plot as 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 BY ME
}
plot(fit, xvar="lambda")
lbs_fun(fit)
但是,我发现情节和图例中的文本标签不匹配.变量am"的颜色显然不正确.错误在哪里?感谢您的帮助.
However, I am getting mismatch between text labels on plot and in legend. The variable 'am' is clearly incorrectly colored. Where is the error? Thanks for your help.
推荐答案
plot(fit, xvar="lambda")
利用函数matplot
.默认情况下,matplot
使用 6 种颜色并回收它们.因此,您必须相应地创建图例:
plot(fit, xvar="lambda")
utilizes the function matplot
. By default, matplot
uses 6 colors and recycles them. So you have to create the 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) # only 6 colors
}
这篇关于R中glmnet图的图例标签错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文