class: center, middle, inverse, title-slide # Exploded code ## Using flipbookr and xaringan ### Me --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 40%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; } </style> # existing ggplot2 plotting observations and linear model is fairly easy with existing ggplot2 code. --- count: false .panel1-start-auto[ ```r *library(tidyverse) ``` ] .panel2-start-auto[ ] --- count: false .panel1-start-auto[ ```r library(tidyverse) *anscombe ``` ] .panel2-start-auto[ ``` x1 x2 x3 x4 y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 4 9 9 9 8 8.81 8.77 7.11 8.84 5 11 11 11 8 8.33 9.26 7.81 8.47 6 14 14 14 8 9.96 8.10 8.84 7.04 7 6 6 6 8 7.24 6.13 6.08 5.25 8 4 4 4 19 4.26 3.10 5.39 12.50 9 12 12 12 8 10.84 9.13 8.15 5.56 10 7 7 7 8 4.82 7.26 6.42 7.91 11 5 5 5 8 5.68 4.74 5.73 6.89 ``` ] --- count: false .panel1-start-auto[ ```r library(tidyverse) anscombe %>% * ggplot() ``` ] .panel2-start-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/start_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r library(tidyverse) anscombe %>% ggplot() + * aes(x = x3, y = y3) ``` ] .panel2-start-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/start_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r library(tidyverse) anscombe %>% ggplot() + aes(x = x3, y = y3) + * geom_point() ``` ] .panel2-start-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/start_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r library(tidyverse) anscombe %>% ggplot() + aes(x = x3, y = y3) + geom_point() + * geom_smooth(method = lm, * se = F) ``` ] .panel2-start-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/start_auto_06_output-1.png)<!-- --> ] <style> .panel1-start-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-start-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-start-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> # plotting predictions plotting the predicted values of the observations is a bit harder, but we can do it with pre calculation. --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted *lm(formula = anscombe$y3 ~ anscombe$x3) ``` ] .panel2-lm-auto[ ``` Call: lm(formula = anscombe$y3 ~ anscombe$x3) Coefficients: (Intercept) anscombe$x3 3.0025 0.4997 ``` ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> * my_model ``` ] .panel2-lm-auto[ ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model *data.frame(x3 = anscombe$x3, * predicted_y3 = my_model$fitted.values) ``` ] .panel2-lm-auto[ ``` x3 predicted_y3 1 10 7.999727 2 8 7.000273 3 13 9.498909 4 9 7.500000 5 11 8.499455 6 14 9.998636 7 6 6.000818 8 4 5.001364 9 12 8.999182 10 7 6.500545 11 5 5.501091 ``` ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> * fitted_data ``` ] .panel2-lm-auto[ ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data *anscombe ``` ] .panel2-lm-auto[ ``` x1 x2 x3 x4 y1 y2 y3 y4 1 10 10 10 8 8.04 9.14 7.46 6.58 2 8 8 8 8 6.95 8.14 6.77 5.76 3 13 13 13 8 7.58 8.74 12.74 7.71 4 9 9 9 8 8.81 8.77 7.11 8.84 5 11 11 11 8 8.33 9.26 7.81 8.47 6 14 14 14 8 9.96 8.10 8.84 7.04 7 6 6 6 8 7.24 6.13 6.08 5.25 8 4 4 4 19 4.26 3.10 5.39 12.50 9 12 12 12 8 10.84 9.13 8.15 5.56 10 7 7 7 8 4.82 7.26 6.42 7.91 11 5 5 5 8 5.68 4.74 5.73 6.89 ``` ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% * ggplot() ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% ggplot() + * aes(x = x3, y = y3) ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% ggplot() + aes(x = x3, y = y3) + * geom_point() ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% ggplot() + aes(x = x3, y = y3) + geom_point() + * geom_smooth(method = lm, * se = F) ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% ggplot() + aes(x = x3, y = y3) + geom_point() + geom_smooth(method = lm, se = F) + * geom_point(data = fitted_data, * aes(x = x3, * y = predicted_y3), * color = "blue") ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-lm-auto[ ```r # calc fitted and add points at fitted lm(formula = anscombe$y3 ~ anscombe$x3) -> my_model data.frame(x3 = anscombe$x3, predicted_y3 = my_model$fitted.values) -> fitted_data anscombe %>% ggplot() + aes(x = x3, y = y3) + geom_point() + geom_smooth(method = lm, se = F) + geom_point(data = fitted_data, aes(x = x3, y = predicted_y3), color = "blue") + * geom_text(data = fitted_data, * aes(x = x3, * y = predicted_y3, * label = round(predicted_y3, * digits = 1) * ), * color = "midnightblue") ``` ] .panel2-lm-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/lm_auto_11_output-1.png)<!-- --> ] <style> .panel1-lm-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-lm-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-lm-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> # Or extend ggplot2 --- count: false .panel1-extend-auto[ ```r *StatOlsfittedpoint <- ggplot2::ggproto(`_class` = "StatOlsfittedpoint", * `_inherit` = ggplot2::Stat, * required_aes = c("x", "y"), * compute_group = function(data, scales) { * model <- lm(formula = data$y ~ data$x) * data.frame(x = data$x, * y = model$fitted.values) * } *) ``` ] .panel2-extend-auto[ ] --- count: false .panel1-extend-auto[ ```r StatOlsfittedpoint <- ggplot2::ggproto(`_class` = "StatOlsfittedpoint", `_inherit` = ggplot2::Stat, required_aes = c("x", "y"), compute_group = function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values) } ) *geom_point_lm_fitted <- function(mapping = NULL, data = NULL, * position = "identity", na.rm = FALSE, * show.legend = NA, * inherit.aes = TRUE, ...) { * ggplot2::layer( * stat = StatOlsfittedpoint, * geom = ggplot2::GeomPoint, * data = data, * mapping = mapping, * position = position, * show.legend = show.legend, * inherit.aes = inherit.aes, * params = list(na.rm = na.rm, ...) * ) *} ``` ] .panel2-extend-auto[ ] <style> .panel1-extend-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-extend-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-extend-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-use-auto[ ```r *library(ggplot2) ``` ] .panel2-use-auto[ ] --- count: false .panel1-use-auto[ ```r library(ggplot2) *ggplot(cars) ``` ] .panel2-use-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-use-auto[ ```r library(ggplot2) ggplot(cars) + * aes(x = speed, y = dist) ``` ] .panel2-use-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-use-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + * geom_point() ``` ] .panel2-use-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-use-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + * geom_smooth(method = lm) ``` ] .panel2-use-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-use-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + geom_smooth(method = lm) + * geom_point_lm_fitted(color = "plum4") ``` ] .panel2-use-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use_auto_06_output-1.png)<!-- --> ] <style> .panel1-use-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-use-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-use-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-extend2-auto[ ```r *StatOlsfitted <- ggplot2::ggproto(`_class` = "StatOlsfitted", * `_inherit` = ggplot2::Stat, * required_aes = c("x", "y"), * compute_group = function(data, scales) { * model <- lm(formula = data$y ~ data$x) * data.frame(x = data$x, * y = model$fitted.values, * label = * round(x = model$fitted.values, * digits = 2)) * } *) ``` ] .panel2-extend2-auto[ ] --- count: false .panel1-extend2-auto[ ```r StatOlsfitted <- ggplot2::ggproto(`_class` = "StatOlsfitted", `_inherit` = ggplot2::Stat, required_aes = c("x", "y"), compute_group = function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } ) *geom_text_lm_fitted <- function(mapping = NULL, data = NULL, * position = "identity", na.rm = FALSE, * show.legend = NA, * inherit.aes = TRUE, ...) { * ggplot2::layer( * stat = StatOlsfitted, * geom = ggplot2::GeomText, * data = data, * mapping = mapping, * position = position, * show.legend = show.legend, * inherit.aes = inherit.aes, * params = list(na.rm = na.rm, ...) * ) *} ``` ] .panel2-extend2-auto[ ] <style> .panel1-extend2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-extend2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-extend2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-use2-auto[ ```r *library(ggplot2) ``` ] .panel2-use2-auto[ ] --- count: false .panel1-use2-auto[ ```r library(ggplot2) *ggplot(cars) ``` ] .panel2-use2-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-use2-auto[ ```r library(ggplot2) ggplot(cars) + * aes(x = speed, y = dist) ``` ] .panel2-use2-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-use2-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + * geom_point() ``` ] .panel2-use2-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-use2-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + * geom_smooth(method = lm) ``` ] .panel2-use2-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-use2-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + geom_smooth(method = lm) + * geom_text_lm_fitted(color = "plum4") ``` ] .panel2-use2-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2_auto_06_output-1.png)<!-- --> ] <style> .panel1-use2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-use2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-use2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> # Finally extension following TLP's advise - write out the data transformation function separately In the video, Thomas Lin Pederson advises pulling out the data transformation function. --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed *my_compute_group <- function(data, scales) { * model <- lm(formula = data$y ~ data$x) * data.frame(x = data$x, * y = model$fitted.values, * label = * round(x = model$fitted.values, * digits = 2)) *} ``` ] .panel2-extendfollowTLPadvise-auto[ ] --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed my_compute_group <- function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } # lets see how this function works... *cars ``` ] .panel2-extendfollowTLPadvise-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed my_compute_group <- function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } # lets see how this function works... cars %>% * transmute(x = speed, * y = dist) ``` ] .panel2-extendfollowTLPadvise-auto[ ``` x y 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed my_compute_group <- function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } # lets see how this function works... cars %>% transmute(x = speed, y = dist) %>% *my_compute_group(data = .) ``` ] .panel2-extendfollowTLPadvise-auto[ ``` x y label 1 4 -1.849460 -1.85 2 4 -1.849460 -1.85 3 7 9.947766 9.95 4 7 9.947766 9.95 5 8 13.880175 13.88 6 9 17.812584 17.81 7 10 21.744993 21.74 8 10 21.744993 21.74 9 10 21.744993 21.74 10 11 25.677401 25.68 11 11 25.677401 25.68 12 12 29.609810 29.61 13 12 29.609810 29.61 14 12 29.609810 29.61 15 12 29.609810 29.61 16 13 33.542219 33.54 17 13 33.542219 33.54 18 13 33.542219 33.54 19 13 33.542219 33.54 20 14 37.474628 37.47 21 14 37.474628 37.47 22 14 37.474628 37.47 23 14 37.474628 37.47 24 15 41.407036 41.41 25 15 41.407036 41.41 26 15 41.407036 41.41 27 16 45.339445 45.34 28 16 45.339445 45.34 29 17 49.271854 49.27 30 17 49.271854 49.27 31 17 49.271854 49.27 32 18 53.204263 53.20 33 18 53.204263 53.20 34 18 53.204263 53.20 35 18 53.204263 53.20 36 19 57.136672 57.14 37 19 57.136672 57.14 38 19 57.136672 57.14 39 20 61.069080 61.07 40 20 61.069080 61.07 41 20 61.069080 61.07 42 20 61.069080 61.07 43 20 61.069080 61.07 44 22 68.933898 68.93 45 23 72.866307 72.87 46 24 76.798715 76.80 47 24 76.798715 76.80 48 24 76.798715 76.80 49 24 76.798715 76.80 50 25 80.731124 80.73 ``` ] --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed my_compute_group <- function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } # lets see how this function works... cars %>% transmute(x = speed, y = dist) %>% my_compute_group(data = .) *StatOlsfitted <- ggplot2::ggproto(`_class` = "StatOlsfitted", * `_inherit` = ggplot2::Stat, * required_aes = c("x", "y"), * compute_group = my_compute_group *) ``` ] .panel2-extendfollowTLPadvise-auto[ ``` x y label 1 4 -1.849460 -1.85 2 4 -1.849460 -1.85 3 7 9.947766 9.95 4 7 9.947766 9.95 5 8 13.880175 13.88 6 9 17.812584 17.81 7 10 21.744993 21.74 8 10 21.744993 21.74 9 10 21.744993 21.74 10 11 25.677401 25.68 11 11 25.677401 25.68 12 12 29.609810 29.61 13 12 29.609810 29.61 14 12 29.609810 29.61 15 12 29.609810 29.61 16 13 33.542219 33.54 17 13 33.542219 33.54 18 13 33.542219 33.54 19 13 33.542219 33.54 20 14 37.474628 37.47 21 14 37.474628 37.47 22 14 37.474628 37.47 23 14 37.474628 37.47 24 15 41.407036 41.41 25 15 41.407036 41.41 26 15 41.407036 41.41 27 16 45.339445 45.34 28 16 45.339445 45.34 29 17 49.271854 49.27 30 17 49.271854 49.27 31 17 49.271854 49.27 32 18 53.204263 53.20 33 18 53.204263 53.20 34 18 53.204263 53.20 35 18 53.204263 53.20 36 19 57.136672 57.14 37 19 57.136672 57.14 38 19 57.136672 57.14 39 20 61.069080 61.07 40 20 61.069080 61.07 41 20 61.069080 61.07 42 20 61.069080 61.07 43 20 61.069080 61.07 44 22 68.933898 68.93 45 23 72.866307 72.87 46 24 76.798715 76.80 47 24 76.798715 76.80 48 24 76.798715 76.80 49 24 76.798715 76.80 50 25 80.731124 80.73 ``` ] --- count: false .panel1-extendfollowTLPadvise-auto[ ```r # this function takes in a dataframe with columns named x and y # and returns a data frame with columns named x y and label # for the output, the x column is the same as the input # the y column is computed based on a fit of the input x and y # and the label is also computed my_compute_group <- function(data, scales) { model <- lm(formula = data$y ~ data$x) data.frame(x = data$x, y = model$fitted.values, label = round(x = model$fitted.values, digits = 2)) } # lets see how this function works... cars %>% transmute(x = speed, y = dist) %>% my_compute_group(data = .) StatOlsfitted <- ggplot2::ggproto(`_class` = "StatOlsfitted", `_inherit` = ggplot2::Stat, required_aes = c("x", "y"), compute_group = my_compute_group ) *geom_text_lm_fitted <- function(mapping = NULL, data = NULL, * position = "identity", na.rm = FALSE, * show.legend = NA, * inherit.aes = TRUE, ...) { * ggplot2::layer( * stat = StatOlsfitted, * geom = ggplot2::GeomText, * data = data, * mapping = mapping, * position = position, * show.legend = show.legend, * inherit.aes = inherit.aes, * params = list(na.rm = na.rm, ...) * ) *} ``` ] .panel2-extendfollowTLPadvise-auto[ ``` x y label 1 4 -1.849460 -1.85 2 4 -1.849460 -1.85 3 7 9.947766 9.95 4 7 9.947766 9.95 5 8 13.880175 13.88 6 9 17.812584 17.81 7 10 21.744993 21.74 8 10 21.744993 21.74 9 10 21.744993 21.74 10 11 25.677401 25.68 11 11 25.677401 25.68 12 12 29.609810 29.61 13 12 29.609810 29.61 14 12 29.609810 29.61 15 12 29.609810 29.61 16 13 33.542219 33.54 17 13 33.542219 33.54 18 13 33.542219 33.54 19 13 33.542219 33.54 20 14 37.474628 37.47 21 14 37.474628 37.47 22 14 37.474628 37.47 23 14 37.474628 37.47 24 15 41.407036 41.41 25 15 41.407036 41.41 26 15 41.407036 41.41 27 16 45.339445 45.34 28 16 45.339445 45.34 29 17 49.271854 49.27 30 17 49.271854 49.27 31 17 49.271854 49.27 32 18 53.204263 53.20 33 18 53.204263 53.20 34 18 53.204263 53.20 35 18 53.204263 53.20 36 19 57.136672 57.14 37 19 57.136672 57.14 38 19 57.136672 57.14 39 20 61.069080 61.07 40 20 61.069080 61.07 41 20 61.069080 61.07 42 20 61.069080 61.07 43 20 61.069080 61.07 44 22 68.933898 68.93 45 23 72.866307 72.87 46 24 76.798715 76.80 47 24 76.798715 76.80 48 24 76.798715 76.80 49 24 76.798715 76.80 50 25 80.731124 80.73 ``` ] <style> .panel1-extendfollowTLPadvise-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-extendfollowTLPadvise-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-extendfollowTLPadvise-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-use2TLP-auto[ ```r *library(ggplot2) ``` ] .panel2-use2TLP-auto[ ] --- count: false .panel1-use2TLP-auto[ ```r library(ggplot2) *ggplot(cars) ``` ] .panel2-use2TLP-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2TLP_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-use2TLP-auto[ ```r library(ggplot2) ggplot(cars) + * aes(x = speed, y = dist) ``` ] .panel2-use2TLP-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2TLP_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-use2TLP-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + * geom_point() ``` ] .panel2-use2TLP-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2TLP_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-use2TLP-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + * geom_smooth(method = lm) ``` ] .panel2-use2TLP-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2TLP_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-use2TLP-auto[ ```r library(ggplot2) ggplot(cars) + aes(x = speed, y = dist) + geom_point() + geom_smooth(method = lm) + * geom_text_lm_fitted(color = "plum4") ``` ] .panel2-use2TLP-auto[ ![](geom_text_lm_fitted_exploded_files/figure-html/use2TLP_auto_06_output-1.png)<!-- --> ] <style> .panel1-use2TLP-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-use2TLP-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-use2TLP-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>