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 myautoplot
here 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)
Nguồn
2017-07-24 13:32:47
Đ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
@ 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
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