Search code examples
rgraphicsplotaxes

How to move X axis from top to bottom on coefficient plot in R (coefplot)?


I have written some code to produce a plot of some logistic regression coefficients and their associated confidence intervals. I have stored these in vectors rather than plotting directly from glm as the data I am using is not my own. Below is the code. I would like the plot to remain horizontal, however I would really like the x axis labels and tick marks to be at the bottom of the plot rather than the top. I have searched the web high and low for an answer to this, but can't seem to find a solution. Any advice would be much appreciated.

require(arm)
coef.vect <- c(0.3, 1.5, 2.2, 0.5)
CI.vect <- c(0.15, 0.20, 0.22, 0.16)
longnames <- c("var1", "var2", "var3", "var4")
coefplot (coef.vect, CI.vect, varnames=FALSE, main="Regression Coefficients")
for (i in 1:length(coef.vect)) { text(coef.vect[i], i, longnames[i], cex = .8, pos = 3)}

Solution

  • you can try to not plot the x axis and then add it, at the bottom, with axis:

    coefplot (coef.vect, CI.vect, varnames=FALSE, main="Regression Coefficients", h.axis=F)
    axis(side=1, line=-1)
    

    by the way, you don't need the for loop here, you can do directly:

    text(coef.vect, seq(coef.vect), longnames, cex = .8, pos = 3)
    

    enter image description here