2017-07-07 18 views
12

Sử dụng autoplot từ ggfortify để tạo ra lô chẩn đoán:Thay đổi tiêu đề trục cho autoplot

library(ggplot2) 
library(ggfortify) 

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

Có thể thay đổi tiêu đề trục và cốt truyện (dễ dàng)? Tôi muốn dịch chúng.

enter image description here

Trả lời

5

Chức năng autoplot.lm trả về một đối tượng S4 (lớp ggmultiplot, xem ?`ggmultiplot-class`). Nếu bạn nhìn vào helpfile, bạn sẽ thấy chúng có các phương pháp thay thế cho các ô riêng lẻ. Điều đó có nghĩa là bạn có thể trích xuất một ô riêng lẻ, sửa đổi nó và đặt nó trở lại. Ví dụ:

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) 

Ở đây tôi chỉ sửa đổi nhãn trục, nhưng bạn thay đổi bất kỳ điều gì về ô riêng lẻ (chủ đề, màu sắc, tiêu đề, kích thước).

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

Vui lòng đừng quên để cho chúng tôi biết nếu nó giúp :)

+0

Điều này hoạt động tốt cho một cốt truyện, nhưng vì mỗi ô phải được tạo riêng, cách kết hợp chúng? 'grid.arrange' cũng không' của 'cowplot'' plot_grid' hoạt động do lớp của đối tượng 'autoplot'. – beetroot

+3

@ củ cải đường; bạn có thể lặp qua các con số tùy biến nhãn. 'm <- mapply (chức năng (w, x, y, z) autoplot (mod, = w) + labs (x = x, y = y, tiêu đề = z), w = 1: 6, x = paste0 ("x", 1: 6), y = paste0 ("y", 1: 6), z = paste0 ("tit", 1: 6)) '. Có lẽ một cách đơn giản hơn để kết hợp chúng, nhưng điều này dường như hoạt động: 'l <- do.call (c, lapply (m, hàm (x) x @ lô)); gridExtra :: grid.arrange (grobs = l, ncol = 2) ' – user20650

+2

dường như bạn cũng có thể thực hiện' p [1,1] <- p [1,1] + labs (x = "nhãn x trục của fig1" , y = "y-trục nhãn của fig1", tiêu đề = "Fig1 cốt truyện") ', mà có thể cung cấp cho một tuyến đường (p là autoplot gốc) – user20650

5

Các giải pháp bởi @ user20650 đề xuất là thú vị và tao nhã.

Đây là giải pháp ít thanh lịch hơn dựa trên myautoplot, phiên bản đã sửa đổi của autoplot. Tôi hy vọng nó có thể giúp bạn.
Tải xuống chức năng myautoplothere và lưu nó vào thư mục làm việc của bạn với tên myautoplot.r.
Sau đó, sử dụng đoạn mã sau:

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

Các vấn đề liên quan