无论在ggPlot中传递什么,在完成绘制之前,首先将其传递到函数fortify
中.下面来看一下ggploy函数:
ggplot2:::ggplot.default
function (data = NULL, mapping = aes(), ..., environment = parent.frame())
{
if (!missing(mapping) && !inherits(mapping, "uneval")) {
cli::cli_abort(c("{.arg mapping} should be created with {.fn aes}.",
x = "You've supplied a {.cls {class(mapping)[1]}} object"))
}
data <- fortify(data, ...)
....
该函数fortify
是通用的,并且它确实包含用于线性模型对象的方法,其定义如下:
ggplot2:::fortify.lm
function (model, data = model$model, ...)
{
infl <- stats::influence(model, do.coef = FALSE)
data$.hat <- infl$hat
data$.sigma <- infl$sigma
data$.cooksd <- stats::cooks.distance(model, infl)
data$.fitted <- stats::predict(model)
data$.resid <- stats::resid(model)
data$.stdresid <- stats::rstandard(model, infl)
data
}
请注意Fortify如何创建所有变量:
艾格
head(fortify(lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)))
Ozone Solar.R Wind Temp .hat .sigma .cooksd .fitted .resid .stdresid
1 41 190 7.4 67 0.04213526 21.26578 1.619281e-03 33.045483 7.9545175 0.38372526
2 36 118 8.0 72 0.02386496 21.28020 1.399323e-05 34.998710 1.0012902 0.04784798
3 12 149 12.6 74 0.01493584 21.24339 1.410343e-03 24.822814 -12.8228139 -0.60997176
4 18 313 11.5 62 0.07184509 21.28037 1.049577e-05 18.475226 -0.4752262 -0.02328889
7 23 299 8.6 65 0.06645355 21.26005 3.644684e-03 32.261431 -9.2614315 -0.45255232
8 19 99 13.8 59 0.04581198 21.12342 1.888167e-02 -6.949919 25.9499188 1.25423141
现在,这个数据集就是用于绘图的数据集.