Search code examples
rgraphicslabelaxisvisreg

Is there a way to rotate the x-axis labels in the visreg package?


*** EDIT **** Figured it out: scales = list(rot = 90)

I am using the visreg() function to plot a linear model comparing bird weights between years. I have three different species, so the graph is very crowded. I want to rotate the x-axis labels so they are perpendicular, but I cannot figure out how to do it. I have used las = 2 in numerous locations, to no avail. Help would be appreciated! Thank you.

 visreg(hawk_lm1, xvar = "Year", by = "Species", whitespace = 0.4, las = 2,
       points.par = list(cex = 0.5, col = "grey")) 

A screenshot of the graph as it is at the moment


Solution

  • According to the documentation of the visreg package, if you use the by= argument the plot is made using the lattice package. In Lattice you can use the scales= argument alongside rot() to rotate the axis, just include the parameter in the visreg function as an additional argument and it should do the job. I created this example from the airquality dataset to illustrate.

    library(visreg)
    airquality$Heat <- cut(airquality$Temp, 3, labels=c("Cool", "Mild", "Hot"))
    fit <- lm(Ozone ~ Solar.R + Wind + Heat, data=airquality)
    
    ##default  x axis labels
    visreg(fit, "Wind", by="Heat",  bty="n", ylab="Ozone")
    
    ##rotated x  axis labels
    visreg(fit, "Wind", by="Heat",  bty="n", ylab="Ozone", scales=list(x=list(rot=90)))