Changing axis titles for autoplot

erc picture erc · Jul 7, 2017 · Viewed 8.6k times · Source

Using autoplot from ggfortify to create diagnostic plots:

library(ggplot2)
library(ggfortify)

mod <- lm(Petal.Width ~ Petal.Length, data = iris)
autoplot(mod, label.size = 3)

Is it possible to change the axis and plot titles (easily)? I'd like to translate them.

enter image description here

Answer

Vandenman picture Vandenman · Jul 25, 2017

The function autoplot.lm returns an S4 object (class ggmultiplot, see ?`ggmultiplot-class`). If you look at the helpfile, you'll see they have replacement methods for individual plots. That means you can extract an individual plot, modify it, and put it back. For example:

library(ggplot2)
library(ggfortify)

mod <- lm(Petal.Width ~ Petal.Length, data = iris)
g <- autoplot(mod, label.size = 3) # store the ggmultiplot object

# new x and y labels
xLabs <- yLabs <- c("a", "b", "c", "d")

# loop over all plots and modify each individually
for (i in 1:4)
    g[i] <- g[i] + xlab(xLabs[i]) + ylab(yLabs[i])

# display the new plot
print(g) 

Here I only modified the axis labels, but you change anything about the plots individually (themes, colors, titles, sizes).