更改自动绘图的轴标题
Changing axis titles for autoplot
使用 ggfortify
中的 autoplot
创建诊断图:
library(ggplot2)
library(ggfortify)
mod <- lm(Petal.Width ~ Petal.Length, data = iris)
autoplot(mod, label.size = 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")
如果对您有帮助,请不要忘记告诉我们:)
@user20650提出的解决方案很有趣也很优雅。
这是一个不太优雅的解决方案,它基于 myautoplot
,autoplot
的修改版本。希望能帮到你。
下载 myautoplot
函数 here 并将其保存在您的工作目录中,名称为 myautoplot.r
.
然后,使用以下代码:
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)
函数autoplot.lm
returns an S4 object(class ggmultiplot,参见?`ggmultiplot-class`
)。如果您查看帮助文件,您会看到它们有针对各个图的替换方法。这意味着您可以提取单个图,对其进行修改,然后再放回去。例如:
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)
这里我只修改了轴标签,但你可以单独更改图表的任何内容(主题、颜色、标题、大小)。
使用 ggfortify
中的 autoplot
创建诊断图:
library(ggplot2)
library(ggfortify)
mod <- lm(Petal.Width ~ Petal.Length, data = iris)
autoplot(mod, label.size = 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")
如果对您有帮助,请不要忘记告诉我们:)
@user20650提出的解决方案很有趣也很优雅。
这是一个不太优雅的解决方案,它基于 myautoplot
,autoplot
的修改版本。希望能帮到你。
下载 myautoplot
函数 here 并将其保存在您的工作目录中,名称为 myautoplot.r
.
然后,使用以下代码:
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)
函数autoplot.lm
returns an S4 object(class ggmultiplot,参见?`ggmultiplot-class`
)。如果您查看帮助文件,您会看到它们有针对各个图的替换方法。这意味着您可以提取单个图,对其进行修改,然后再放回去。例如:
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)
这里我只修改了轴标签,但你可以单独更改图表的任何内容(主题、颜色、标题、大小)。