11# ' Add Residual Lines to a Plot
22# '
3- # ' This function adds vertical lines representing residuals from a linear model to a ggformula plot.
3+ # ' This function adds vertical lines representing residuals from a linear model to a ggformula plot.
44# ' The residuals are drawn from the observed data points to the predicted values from the model.
55# '
66# ' @param plot A ggformula plot object, typically created with `gf_point()`.
77# ' @param model A fitted linear model object created using `lm()`.
88# ' @param linewidth A numeric value specifying the width of the residual lines. Default is `0.2`.
9- # ' @param ... Additional aesthetics passed to `geom_segment()`, such as `color`, `alpha`, `linetype`.
9+ # ' @param ... Additional aesthetics passed to `geom_segment()`, such as `color`, `alpha`,
10+ # ' `linetype`.
1011# '
1112# ' @return A ggplot object with residual lines added.
1213# '
1314# ' @export
1415# ' @examples
16+ # ' Height_model <- lm(Thumb ~ Height, data = Fingers)
1517# ' gf_point(Thumb ~ Height, data = Fingers) %>%
1618# ' gf_model(Height_model) %>%
1719# ' gf_resid(Height_model, color = "red", alpha = 0.5)
@@ -22,9 +24,9 @@ gf_resid <- function(plot, model, linewidth = 0.2, ...) {
2224
2325 # Get model predictions and residuals and assign them to the model data
2426 model_data <- model $ model
25- model_data $ prediction <- predict(model )
26- model_data $ residual <- resid(model )
27-
27+ model_data $ prediction <- stats :: predict(model )
28+ model_data $ residual <- stats :: resid(model )
29+
2830 # Access the x and y coordinates used in the plot
2931 plot_data <- ggplot_build(plot )$ data [[1 ]]
3032 x_loc <- plot_data $ x
@@ -33,73 +35,77 @@ gf_resid <- function(plot, model, linewidth = 0.2, ...) {
3335 # Ensures same jitter as the x and y coord from plot
3436 set.seed(rand_int )
3537 plot +
36- geom_segment(aes(
37- x = x_loc ,
38- y = model_data $ prediction ,
39- xend = x_loc ,
40- yend = y_loc
41- ),
42- inherit.aes = TRUE ,
43- linewidth = linewidth ,
44- ...
38+ geom_segment(
39+ aes(
40+ x = x_loc ,
41+ y = model_data $ prediction ,
42+ xend = x_loc ,
43+ yend = y_loc
44+ ),
45+ inherit.aes = TRUE ,
46+ linewidth = linewidth ,
47+ ...
4548 )
4649}
4750
4851# ' Add Squared Residual Visualization to a Plot
4952# '
50- # ' This function adds squared residual representations to a ggformula plot, illustrating squared error as a polygon.
51- # ' The function dynamically adjusts the aspect ratio to ensure proper scaling of squares.
53+ # ' This function adds squared residual representations to a ggformula plot, illustrating
54+ # ' squared error as a polygon. The function dynamically adjusts the aspect ratio to ensure
55+ # ' proper scaling of squares.
5256# '
5357# ' @param plot A ggformula plot object, typically created with `gf_point()`.
5458# ' @param model A fitted linear model object created using `lm()`.
5559# ' @param aspect A numeric value controlling the square's aspect ratio. Default is `4/6`.
56- # ' @param alpha A numeric value specifying the transparency level of the polygon fill. Default is `0.1`.
60+ # ' @param alpha A numeric value specifying the transparency of the square's fill. Default is `0.1`.
5761# ' @param ... Additional aesthetics passed to `geom_polygon()`, such as `color` and `fill`.
5862# '
5963# ' @return A ggplot object with squared residuals added.
6064# '
6165# ' @export
6266# ' @examples
67+ # ' Height_model <- lm(Thumb ~ Height, data = Fingers)
6368# ' gf_point(Thumb ~ Height, data = Fingers) %>%
6469# ' gf_model(Height_model) %>%
6570# ' gf_squaresid(Height_model, color = "blue", alpha = 0.5)
66- gf_squaresid <- function (plot , model , aspect = 4 / 6 , alpha = 0.1 , ... ) {
71+ gf_squaresid <- function (plot , model , aspect = 4 / 6 , alpha = 0.1 , ... ) {
6772 # Handles random jitter
6873 rand_int <- sample(1 : 100 , 1 )
6974 set.seed(rand_int )
70-
75+
7176 # Get model predictions and residuals and assign them to the model data
7277 model_data <- model $ model
73- model_data $ prediction <- predict(model )
74- model_data $ residual <- resid(model )
75-
78+ model_data $ prediction <- stats :: predict(model )
79+ model_data $ residual <- stats :: resid(model )
80+
7681 # Access the x and y coordinates used in the plot
7782 plot_data <- ggplot_build(plot )$ data [[1 ]]
7883 model_data $ x_loc <- plot_data $ x
7984 model_data $ y_loc <- plot_data $ y
80-
85+
8186 # Access the range of x and y used in the panel
8287 plot_layout <- ggplot_build(plot )$ layout
8388 panel_params <- plot_layout $ panel_params [[1 ]]
8489 x_range <- panel_params $ x.range
8590 y_range <- panel_params $ y.range
86-
91+
8792 # Compute ratio for proper aspect scaling
8893 range_ratio <- (x_range [2 ] - x_range [1 ]) / (y_range [2 ] - y_range [1 ])
8994 model_data $ dir <- ifelse(model_data $ x_loc > mean(x_range ), - 1 , 1 )
90- model_data $ adj_side <- model_data $ x_loc + model_data $ dir * abs(model_data $ residual * aspect * range_ratio )
91-
95+ side_length <- abs(model_data $ residual ) * aspect * range_ratio
96+ model_data $ adj_side <- model_data $ x_loc + model_data $ dir * side_length
97+
9298 # Create a dataframe for plotting polygons
93- squares_data <- do.call(rbind , lapply(1 : nrow(model_data ), function (i ) {
99+ squares_data <- do.call(rbind , lapply(seq_len( nrow(model_data ) ), function (i ) {
94100 resid_side <- model_data $ x_loc [i ]
95101 top <- model_data $ prediction [i ]
96102 bottom <- model_data $ y_loc [i ]
97103 opp_side <- model_data $ adj_side [i ]
98-
104+
99105 data.frame (
100106 x = c(resid_side , opp_side , opp_side , resid_side ),
101107 y = c(bottom , bottom , top , top ),
102- id = i # Unique identifier for each square
108+ id = i # Unique identifier for each square
103109 )
104110 }))
105111
@@ -108,7 +114,7 @@ gf_squaresid <- function(plot, model, aspect = 4/6, alpha = 0.1, ...) {
108114 plot +
109115 geom_polygon(
110116 data = squares_data ,
111- aes(x = x , y = y , group = id ),
117+ aes(x = .data $ x , y = .data $ y , group = .data $ id ),
112118 inherit.aes = FALSE ,
113119 alpha = alpha ,
114120 ...
0 commit comments