Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing axis titles for autoplot

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

like image 309
erc Avatar asked Jul 07 '17 14:07

erc


People also ask

How do I rename axis titles in R?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do I add a title to autoplot?

To add an arbitrary title and/or subtitle to a plot, use either function ggtitle() or function labs() from package 'ggplot2'. For example "title:objt" , the default for "title" , adds a title with the name of the object being plotted.

How do I change axis labels in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How to change the axis labels and title of a plot?

Make sure to assign the axes-level object while creating the plot. This object is then used for setting the title and labels as shown below. We can also change the axis labels and set the plot title with the matplotlib.pyplot object using xlabel (), ylabel () and title () functions.

What is the use of Axis title and caption in plot?

title : To add plot label. subtitle : To add subtitle in the plot. caption : To add caption in the plot. axis.title.x : For horizontal axis. axis.title.y : For vertical axis.

How to change plot titles in ggplot2?

Change plot titles by using the functions ggtitle (), xlab () and ylab () : p + ggtitle("Plot of length n by dose") + xlab("Dose (mg)") + ylab("Teeth length") Note that, you can use n to split long title into multiple lines. Change plot titles using the function labs () as follow :

How do I change the title of a plot in Excel?

Change the main title and axis labels Change plot titles by using the functions ggtitle (), xlab () and ylab () : p + ggtitle("Plot of length \n by dose") + xlab("Dose (mg)") + ylab("Teeth length") Note that, you can use \n to split long title into multiple lines.


2 Answers

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).

like image 89
Vandenman Avatar answered Sep 17 '22 15:09

Vandenman


The solutions proposed by @user20650 are interesting and elegant.

Here is a less elegant solution based on myautoplot, a modified version of autoplot. I hope it can help you.
Download the myautoplot funtion here and save it in your working directory with the name myautoplot.r.
Then, use the following code:

library(ggplot2)
library(ggfortify)

source("myautoplot.r")
mod <- lm(Petal.Width ~ Petal.Length, data = iris)

####
# Define x-labels, y-labels and titles
####
# Residuals vs Fitted Plot
xlab_resfit <- "Xlab ResFit"
ylab_resfit <- "Ylab ResFit"
title_resfit <- "Title ResFit"

# Normal Q-Q Plot
xlab_qqplot <- "Xlab QQ"
ylab_qqplot <- "Ylab QQ"
title_qqplot <- "Title QQ"

# Scale-Location Plot
xlab_scaleloc <- "Xlab S-L"
ylab_scaleloc <- "Ylab S-L"
title_scaleloc <- "Title S-L"

# Cook's distance Plot
xlab_cook <- "Xlab Cook"
ylab_cook <- "Ylab Cook"
title_cook <- "Title Cook"

# Residuals vs Leverage Plot
xlab_reslev <- "Xlab Res-Lev"
ylab_reslev <- "Ylab Res-Lev"
title_reslev <- "Title Res-Lev"

# Cook's dist vs Leverage Plot
xlab_cooklev <- "Xlab Cook-Lev"
ylab_cooklev <- "Ylab Cook-Lev"
title_cooklev <- "Title Cook-Lev"

# Collect axis labels and titles in 3 lists    
xlab_list <- list(resfit=xlab_resfit, qqplot=xlab_qqplot, 
      scaleloc=xlab_scaleloc, cook=xlab_cook, reslev=xlab_reslev,
      cooklev=xlab_cooklev)
ylab_list <- list(resfit=ylab_resfit, qqplot=ylab_qqplot, 
      scaleloc=ylab_scaleloc, cook=ylab_cook, reslev=ylab_reslev,
      cooklev=ylab_cooklev)
title_list <- list(resfit=title_resfit, qqplot=title_qqplot, 
      scaleloc=title_scaleloc, cook=title_cook, reslev=title_reslev,
      cooklev=title_cooklev)

# Pass the lists of axis labels and title to myautoplot
myautoplot(mod, which=1:6, xlab=xlab_list, 
                           ylab=ylab_list, 
                           title=title_list)

enter image description here

like image 33
Marco Sandri Avatar answered Sep 18 '22 15:09

Marco Sandri