[英]Customize checkresiduals() function in R with a `ggtheme` package
我正在用 RMarkdown 写报告。 使用经典的theme_economist()
进行可视化。
我所有的情节都有相同的风格。 例如
但是有一个 function, checkresiduals()
,它不是tidyverse
系列的一部分,我无法使用theme_economist()
对其进行自定义。
结果,它是唯一没有主题的plot,这有点惹恼我的完美主义。
有机会定制吗?
还有其他方法可以使用ggplot2()
plot 残差,但我喜欢 function。 也许有人知道如何定制它? 我很欣赏你的建议!
一个可重复的例子
# Packages
library(tidyverse)
library(forecast)
library(ggthemes)
# Data
data(mtcars)
# Create a plot
ggplot(mtcars, aes(disp, hp)) +
geom_point(color = "red", size = 3) +
labs(title = "I love mtcars",
subtitle = "Do you love mtcars?",
x = "disp",
y = "hp") +
theme_economist()
# Fit a model
mtcars_model <- lm(disp ~ hp, data = mtcars)
# Check residuals
checkresiduals(mtcars_model, color = "red")
Output
看起来不像 function 可以轻松自定义它返回的 ggplots,但由于您只是设置主题,因此可以设置全局主题。
theme_set(theme_economist())
这将更改 session 中所有后续绘图的主题。 或者,您可以编写一个包装器来更改主题,绘制 plot,然后将其更改回来。 例如
with_theme_economist <- function(expr) {
orig <- theme_get()
theme_set(theme_economist())
force(expr)
theme_set(orig)
}
with_theme_economist(checkresiduals(mtcars_model, color = "red"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.