2017-07-07 4 views
12

Mit autoplot von ggfortify Diagnose Plots zu erstellen:Ändern Achsentitel für autoplot

library(ggplot2) 
library(ggfortify) 

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

Ist es möglich, die Achse und Plot-Titel (leicht) zu ändern? Ich würde sie gerne übersetzen.

enter image description here

Antwort

5

Die Funktion autoplot.lm S4 ein Objekt zurückgibt (Klasse ggmultiplot siehe ?`ggmultiplot-class`). Wenn Sie sich die Hilfedatei ansehen, sehen Sie, dass sie Ersatzmethoden für einzelne Diagramme haben. Das heißt, Sie können einen einzelnen Plot extrahieren, modifizieren und zurückstellen. Zum Beispiel:

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) 

Hier modifizierte ich nur die Achsenbeschriftungen, aber sie ändert nichts über die Parzellen einzeln (Themen, Farben, Titel, Größen).

3
library(ggplot2) 
library(ggfortify) 

mod <- lm(Petal.Width ~ Petal.Length, data = iris) 
autoplot(mod,which=c(1:6), ncols=2) #total 6 plots in two columns 
#change axes label & title of plot 1. similarly by changing 'which' parameters count you can label other plots. 
autoplot(mod,which=1) + 
    labs(x="x-axis label of fig1", y="y-axis label of fig1", title="Fig1 plot") 

Bitte vergessen Sie nicht, uns wissen zu lassen, wenn es :) half

+0

Dies funktioniert gut für eine einzelne Handlung, aber wie jede Handlung separat erstellt werden muss, wie man sie kombiniert? weder 'grid.arrange' noch' cowplot's 'plot_grid' funktionieren aufgrund der Klasse des 'autoplot'-Objekts. – beetroot

+3

@beetroot; Sie könnten die Figuren, die Etiketten anpassen, durchlaufen. 'm <- mapply (Funktion (w, x, y, z) Autoplot (mod, was = w) + Labs (x = x, y = y, Titel = z), w = 1: 6, x = paste0 ("x", 1: 6), y = paste0 ("y", 1: 6), z = paste0 ("tit", 1: 6)). Es gibt wahrscheinlich einen einfacheren Weg, sie zu kombinieren, aber das scheint zu funktionieren: 'l <- do.call (c, lapply (m, Funktion (x) x @ Plots)); gridExtra :: grid.arrange (grobs = l, ncol = 2) ' – user20650

+2

scheint auch' p [1,1] <- p [1,1] + labs (x = "x-axis label of fig1" , y = "y-Achsenbeschriftung von fig1", title = "Fig1-Plot"), was vielleicht eine andere Route ergibt (p ist der ursprüngliche Autoplot) – user20650

5

Die vorgeschlagenen Lösungen von @ user20650 sind interessant und elegant.

Hier ist eine weniger elegante Lösung basierend auf myautoplot, eine modifizierte Version von autoplot. Ich hoffe, es kann dir helfen.
Laden Sie die myautoplot Funktion here herunter und speichern Sie sie in Ihrem Arbeitsverzeichnis unter dem Namen myautoplot.r.
Dann verwenden den folgenden 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

Verwandte Themen