Search code examples
rplotglmnet

Remove upper axis in plot glmnet


When using glmnet and making a plot of the coefficient path, I would like to remove the axis above the plot.

How do you remove this axis? I found this toy example on the web:

library(glmnet)
age <- c(4,8,7,12,6,9,10,14,7) 
gender <- c(1,0,1,1,1,0,1,0,0) ; gender<-as.factor(gender)
bmi_p <- c(0.86,0.45,0.99,0.84,0.85,0.67,0.91,0.29,0.88) 
m_edu <- c(0,1,1,2,2,3,2,0,1); m_edu<-as.factor(m_edu)
p_edu <- c(0,2,2,2,2,3,2,0,0); p_edu<-as.factor(p_edu)
f_color <- c("blue", "blue", "yellow", "red", "red", "yellow", "yellow", "red", "yellow")
asthma <- c(1,1,0,1,0,0,0,1,1)
f_color <- as.factor(f_color)
xfactors <- model.matrix(asthma ~ gender + m_edu + p_edu + f_color)[,-1]
x <- as.matrix(data.frame(age, bmi_p, xfactors))
glmmod<-glmnet(x,y=as.factor(asthma),alpha=1,family='binomial')
plot(glmmod,xvar="lambda", axes=FALSE)

Solution

  • This unwanted axis is made with axis function, I guess. We can use this function one more time to cover it. The trick is as follows:

    1) Create a sequence with many points at which tick-marks are to be drawn.

    2) Make them bigger (lengthen upwards)

    3) Enlarge the width of the axis

    4) Change the white colour and switch off labels

    plot(glmmod, xvar = "lambda", axes = F, xlab = "", ylab = "")
    axis(side = 3,
         at = seq(par("usr")[1], par("usr")[2], len = 1000), 
         tck = -0.5,
         lwd = 2, 
         col = "white", 
         labels = F)