### Example Usage Source: https://docs.r4photobiology.info/ggpmisc/reference/scale_y_Pvalue.html Demonstrates how to use `scale_x_logFC` and `scale_y_Pvalue` for plotting, and `scale_y_FDR` with custom limits. ```APIDOC ## Examples ```R set.seed(12346) my.df <- data.frame(x = rnorm(50, sd = 4), y = 10^-runif(50, min = 0, max = 20)) # Example 1: Using scale_x_logFC and scale_y_Pvalue ggplot(my.df, aes(x, y)) + geom_point() + scale_x_logFC() + scale_y_Pvalue() # Example 2: Using scale_y_FDR with custom limits ggplot(my.df, aes(x, y)) + geom_point() + scale_x_logFC() + scale_y_FDR(limits = c(NA, 1e-20)) ``` ``` -------------------------------- ### Example 1: Default label construction Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Demonstrates the default label construction using `use_label()` with `stat_poly_eq`. ```APIDOC ## Example 1: Default label construction # generate artificial data set.seed(4321) x <- 1:100 y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4) my.data <- data.frame(x = x, y = y * 1e-5, group = c("A", "B"), y2 = y * 1e-5 + c(2, 0)) # give a name to a formula formula <- y ~ poly(x, 3, raw = TRUE) # default label constructed by use_label() ggplot(data = my.data, mapping = aes(x = x, y = y2, colour = group)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(mapping = use_label(), formula = formula) ``` -------------------------------- ### Install ggpmisc from GitHub Source: https://docs.r4photobiology.info/ggpmisc/index.html Install the current unstable version of ggpmisc directly from GitHub sources. Ensure the 'remotes' package is installed first. ```r # install.packages("remotes") # nolint: commented_code_linter. remotes::install_github("aphalo/ggpmisc") ``` -------------------------------- ### Example 2: User specified label components Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Shows how to specify custom label components using `use_label()`. ```APIDOC ## Example 2: User specified label components # user specified label components ggplot(data = my.data, mapping = aes(x = x, y = y2, colour = group)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(mapping = use_label("eq", "F"), formula = formula) ``` -------------------------------- ### Numeric Output for stat_quant_eq Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html This example demonstrates obtaining the numeric coefficients of the regression equations from stat_quant_eq. ```R if (gginnards.installed) ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_eq(formula = formula, geom = "debug_group", output.type = "numeric") ``` -------------------------------- ### Load refitME Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Loads the 'refitME' package into the R session. Ensure this package is installed before running. ```R library(refitME) ``` -------------------------------- ### Example 5: Combine other mappings with default labels Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Shows how to combine other aesthetic mappings with default labels generated by `use_label()`. ```APIDOC ## Example 5: Combine other mappings with default labels # combine other mappings with default labels ggplot(data = my.data, mapping = aes(x = x, y = y2)) + geom_point(mapping = aes(colour = group)) + stat_poly_line(mapping = aes(colour = group), formula = formula) + stat_poly_eq(mapping = use_label(aes(colour = group)), formula = formula) ``` -------------------------------- ### Install ggpmisc from R-Universe Source: https://docs.r4photobiology.info/ggpmisc/index.html Install the current unstable version of ggpmisc from the R-Universe repository, which provides binaries for multiple platforms. ```r install.packages("ggpmisc", repos = c("https://aphalo.r-universe.dev", "https://cloud.r-project.org")) ``` -------------------------------- ### Example 4: Combine mapping to label aesthetic with other mappings Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Illustrates combining the `label` aesthetic mapping with other mappings using `use_label()` and `aes()`. ```APIDOC ## Example 4: Combine mapping to label aesthetic with other mappings # combine the mapping to the label aesthetic with other mappings ggplot(data = my.data, mapping = aes(x = x, y = y2)) + geom_point(mapping = aes(colour = group)) + stat_poly_line(mapping = aes(colour = group), formula = formula) + stat_poly_eq(mapping = use_label("grp", "eq", "F", aes(grp.label = group)), formula = formula) ``` -------------------------------- ### Example 3: User specified label components and separator Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Demonstrates specifying custom label components and a custom separator with `use_label()`. ```APIDOC ## Example 3: User specified label components and separator # user specified label components and separator ggplot(data = my.data, mapping = aes(x = x, y = y2, colour = group)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(mapping = use_label("R2", "F", sep = "*\" with \"*"), formula = formula) ``` -------------------------------- ### Example: FC_format with different log bases Source: https://docs.r4photobiology.info/ggpmisc/reference/FC_format.html Demonstrates using FC_format with various log bases for data and labels to format a sequence of numbers. ```R FC_format(2, 10)(1:5) ``` ```R FC_format(0, 2)(c(1/4, 1/2, 1,2,4,8)) ``` ```R FC_format(10, 0)(-1:3) ``` ```R FC_format(0, 10)(c(0.1, 1, 10, 100, 100)) ``` -------------------------------- ### Multiple Quantiles with Text Output Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html This example shows how to specify multiple quantiles (0.25, 0.5, 0.75) and display their corresponding regression equations as text. ```R if (gginnards.installed) ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_eq(formula = formula, quantiles = c(0.25, 0.5, 0.75), geom = "debug_group", output.type = "text") ``` -------------------------------- ### Markdown Output for stat_quant_eq Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html This example shows how to configure stat_quant_eq to output the regression equations in markdown format, suitable for reports. ```R if (gginnards.installed) ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_eq(mapping = aes(label = after_stat(eq.label)), formula = formula, geom = "debug_group", output.type = "markdown") ``` -------------------------------- ### Inspect Residuals with Robust Linear Model (rlm) Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_residuals.html This example shows how to inspect residuals using stat_fit_residuals with the 'rlm' method for robust linear modeling. The 'gginnards' package must be installed and loaded. ```R if (gginnards.installed) ggplot(my.data, aes(x, y)) + stat_fit_residuals(formula = my.formula, method = "rlm", geom = "debug_group") ``` -------------------------------- ### Load 'segmented' Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Loads the 'segmented' package into the R session. Ensure the package is installed before running. ```r library(segmented) ``` -------------------------------- ### Load robustbase package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Loads the 'robustbase' package into the current R session. Ensure this package is installed before loading. ```r library(robustbase) ``` -------------------------------- ### stat_distrmix_eq with components = "members" Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_eq.html Example of using stat_distrmix_eq when components are specified as 'members'. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(components = "members") + stat_distrmix_eq(components = "members") ``` -------------------------------- ### Overlaying histogram with stat_distrmix_line() Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_line.html This example overlays a histogram of the 'waiting' variable with a density fit from stat_distrmix_line(). The components are colored and filled based on the 'component' aesthetic. ```R ggplot(faithful, aes(x = waiting)) + geom_histogram(aes(y = after_stat(density)), bins = 20) + stat_distrmix_line(aes(colour = after_stat(component), fill = after_stat(component)), geom = "area", linewidth = 1, alpha = 0.25, se = FALSE) ``` -------------------------------- ### Install ggpmisc from CRAN Source: https://docs.r4photobiology.info/ggpmisc/index.html Use this command to install the most recent stable version of the ggpmisc package from CRAN. ```r install.packages("ggpmisc") ``` -------------------------------- ### stat_distrmix_line() with components = "sum" Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_line.html This example shows how to use stat_distrmix_line() when fitting a mixture of Normal distributions, specifying that the components should be summed. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(components = "sum") ``` -------------------------------- ### Check output type for a specified type Source: https://docs.r4photobiology.info/ggpmisc/reference/check_output_type.html Passes through the specified output type if it is not NULL. This example shows 'text' as the specified type. ```R check_output_type("text") #> [1] "text" ``` -------------------------------- ### Linear Regression Fit Summary with Defaults Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_tb.html Applies a linear regression model and displays a summary of the fit using default settings. Ensure the 'broom' package is installed. ```R if (broom.installed) library(broom) # data for examples x <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1) covariate <- sqrt(x) + rnorm(9) group <- factor(c(rep("A", 4), rep("B", 5))) my.df <- data.frame(x, group, covariate) gginnards.installed <- requireNamespace("gginnards", quietly = TRUE) if (gginnards.installed) library(gginnards) ## covariate is a numeric or continuous variable # Linear regression fit summary, all defaults if (broom.installed) ggplot(my.df, aes(covariate, x)) + geom_point() + stat_fit_tb() + expand_limits(y = 70) ``` -------------------------------- ### Debug panel for stat_multcomp Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_multcomp.html This example demonstrates how to use stat_multcomp with debug_panel to inspect the intermediate data generated for multiple comparisons. It shows the structure and content of the data before it's used for plotting. ```R stat_multcomp(label.type = "bars", output.type = "numeric", geom = "debug_panel") ``` -------------------------------- ### Basic Polynomial Regression Equation in LaTeX Source: https://docs.r4photobiology.info/ggpmisc/articles/latex-labels.html Displays a polynomial regression equation using LaTeX formatting. Ensure the 'latex2exp' package is installed for rendering. ```R formula <- y ~ poly(x, 3, raw = TRUE) ggplot(my.data, aes(x, y)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(formula = formula, geom = "latex", hjust = 0, vjust = 1) ``` -------------------------------- ### stat_distrmix_line() with components = "members" Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_line.html This example demonstrates using stat_distrmix_line() to fit a mixture of Normal distributions, with the 'components' argument set to 'members' to consider individual components. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(components = "members") ``` -------------------------------- ### stat_quant_band with rqss Method and qss Formula Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_band.html Applies the 'rqss' method with a 'qss(x)' formula to fit an additive model. Note: This example may produce warnings if 'qss' is not available. ```R ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_quant_band(method = "rqss", formula = y ~ qss(x)) ``` -------------------------------- ### Linear Regression Fit Summary with Default Formatting Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_tb.html Applies a linear regression model and displays a summary of the fit using default table formatting. Ensure the 'broom' package is installed. ```R if (broom.installed) ggplot(my.df, aes(covariate, x)) + geom_point() + stat_fit_tb(tb.type = "fit.summary") + expand_limits(y = 70) ``` -------------------------------- ### Example data generation for logFC scales Source: https://docs.r4photobiology.info/ggpmisc/reference/scale_x_logFC.html Generates a data frame with random normal data for demonstrating the usage of log fold change scales. ```R set.seed(12346) my.df <- data.frame(x = rnorm(50, sd = 4), y = rnorm(50, sd = 4)) ``` -------------------------------- ### stat_quant_band with rqss Method and qss(y, constraint = "D") Formula Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_band.html Uses the 'rqss' method with a constrained 'qss(y)' formula. Note: This example may produce warnings if 'qss' is not available. ```R ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_quant_band(method = "rqss", formula = x ~ qss(y, constraint = "D")) ``` -------------------------------- ### Debug Correlation Output in LaTeX Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_correlation.html Use this snippet to get a debug output of the correlation calculation in LaTeX format. Ensure gginnards is installed for this functionality. ```r if (gginnards.installed) ggplot(my.data, aes(x, y)) + geom_point() + stat_correlation(geom = "debug_group", output.type = "LaTeX") ``` -------------------------------- ### Plot with Ternary Shape Scale and Hidden Guide Source: https://docs.r4photobiology.info/ggpmisc/reference/scale_shape_outcome.html Shows how to create a plot with `scale_shape_outcome` while disabling the legend guide. Note: `guide = FALSE` is deprecated; use `guide = "none"`. ```R ggplot(my.df, aes(x, y, shape = outcome3)) + geom_point() + scale_shape_outcome(guide = FALSE) + theme_bw() #> Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in #> ggplot2 3.3.4. #> ℹ Please use "none" instead. ``` -------------------------------- ### Generate artificial data for plotting examples Source: https://docs.r4photobiology.info/ggpmisc/articles/output-types.html Generates a set of artificial data using a fixed seed for reproducibility. This data is suitable for use in subsequent plotting examples. ```R set.seed(4321) ``` -------------------------------- ### Basic stat_distrmix_eq Usage Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_eq.html Demonstrates the default usage of stat_distrmix_eq with stat_distrmix_line. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(components = "sum") + stat_distrmix_eq() ``` -------------------------------- ### Citation for refitME Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Prints the citation details for the 'refitME' package. Use this to properly attribute the package in academic work. ```R print(citation(package = "refitME", auto = TRUE), bibtex = FALSE) ``` -------------------------------- ### Get Citation for lspline Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Prints the citation information for the 'lspline' package. Use this when citing the package in publications. ```r print(citation(package = "lspline", auto = TRUE), bibtex = FALSE) ``` -------------------------------- ### Manually Assembling Labels with sprintf() Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_poly_eq.html Shows how to manually create the label string using sprintf() within aes(). This method offers flexible formatting for R-squared, F-value, and p-value labels. ```R ggplot(my.data, aes(x, y)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(aes(label = sprintf("%s*\" with \"*%s*\" and \"*%s", after_stat(rr.label), after_stat(f.value.label), after_stat(p.value.label))), formula = formula) ``` -------------------------------- ### stat_distrmix_eq with components = "members" and se = TRUE Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_eq.html Demonstrates stat_distrmix_eq with 'members' components and enabling standard error display. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(components = "members") + stat_distrmix_eq(components = "members", se = TRUE) ``` -------------------------------- ### stat_distrmix_line() with k = 1 (single Normal distribution) Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_line.html This example demonstrates the special case where k = 1, fitting a single Normal distribution to a subset of the faithful dataset. Parameters irrelevant to a single distribution fit are ignored. ```R ggplot(subset(faithful, waiting > 66), aes(x = waiting)) + stat_distrmix_line(k = 1) ``` -------------------------------- ### Get Citation for 'splines' Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Prints the citation information for the 'splines' package in R. Use this when citing the package in publications. ```r print(citation(package = "splines", auto = TRUE), bibtex = FALSE) ``` -------------------------------- ### Combine label mapping with other mappings using use_label Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Demonstrates combining the label aesthetic mapping with other aesthetic mappings (e.g., color) using `use_label()` and `aes()`. Requires artificial data and a formula definition. ```R set.seed(4321) x <- 1:100 y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4) my.data <- data.frame(x = x, y = y * 1e-5, group = c("A", "B"), y2 = y * 1e-5 + c(2, 0)) # give a name to a formula formula <- y ~ poly(x, 3, raw = TRUE) # combine the mapping to the label aesthetic with other mappings ggplot(data = my.data, mapping = aes(x = x, y = y2)) + geom_point(mapping = aes(colour = group)) + stat_poly_line(mapping = aes(colour = group), formula = formula) + stat_poly_eq(mapping = use_label("grp", "eq", "F", aes(grp.label = group)), formula = formula) ``` -------------------------------- ### Get Citation for MASS Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Prints the citation information for the MASS package. Use this when publishing research that utilizes the package. ```r print(citation(package = "MASS", auto = TRUE), bibtex = FALSE) ``` -------------------------------- ### Overlaying histogram with stat_distrmix_line() and components = "members" Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_distrmix_line.html This example overlays a histogram with a density fit from stat_distrmix_line(), using 'members' for components and coloring/filling by component. `se = FALSE` is used to disable standard error shading. ```R ggplot(faithful, aes(x = waiting)) + stat_distrmix_line(aes(colour = after_stat(component), fill = after_stat(component)), geom = "area", linewidth = 1, alpha = 0.25, components = "members", se = FALSE) ``` -------------------------------- ### Inspecting stat_quant_band Data with geom_debug_group Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_band.html Uses geom_debug_group from the gginnards package to inspect the data generated by stat_quant_band. Requires gginnards to be installed. ```R gginnards.installed <- requireNamespace("gginnards", quietly = TRUE) if (gginnards.installed) library(gginnards) if (gginnards.installed) ggplot(mpg, aes(displ, hwy)) + stat_quant_band(geom = "debug_group") ``` -------------------------------- ### Add ANOVA Table with Flipped Coordinates Source: https://docs.r4photobiology.info/ggpmisc/articles/user-guide.html Displays an ANOVA table after flipping the coordinate system. This example also adjusts x-axis expansion. ```R ggplot(chickwts, aes(factor(feed), weight)) + stat_summary(fun.data = "mean_se") + stat_fit_tb(tb.type = "fit.anova", label.x = "left", size = 3) + scale_x_discrete(expand = expansion(mult = c(0.2, 0.5))) + coord_flip() ``` -------------------------------- ### Load lspline Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Loads the 'lspline' package into the R session. Ensure this is run before using any functions from the package. ```r library(lspline) ``` -------------------------------- ### Manually Assembling Labels with paste() Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_poly_eq.html Demonstrates how to manually construct the label string for the regression equation using paste() and aes(). This allows for custom formatting of R-squared and n labels. ```R ggplot(my.data, aes(x, y)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(aes(label = paste(after_stat(rr.label), after_stat(n.label), sep = "*\", \"*")), formula = formula) ``` -------------------------------- ### Manually Assemble Label with sprintf() Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html Illustrates manual assembly and mapping of a specific label using sprintf() and aes() for stat_quant_eq. This is an alternative to paste() for custom label formatting. ```R ggplot(my.data, aes(x, y2, color = group, grp.label = group)) + geom_point() + stat_quant_band(method = "rq", formula = formula, quantiles = c(0.05, 0.5, 0.95)) + stat_quant_eq(mapping = aes(label = sprintf("%s*\": \"*%s", after_stat(grp.label), after_stat(eq.label))), quantiles = c(0.05, 0.5, 0.95), formula = formula, size = 3) ``` -------------------------------- ### Get Citation for 'segmented' Package Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Use this to cite the 'segmented' package in publications. It provides the package name, author, year, title, and DOI. ```r print(citation(package = "segmented", auto = TRUE), bibtex = FALSE) ``` -------------------------------- ### Augmenting Fits with nls Source: https://docs.r4photobiology.info/ggpmisc/articles/user-guide.html Fit a non-linear model using `stat_fit_augment` with the 'nls' method. Provide model formula and starting parameters in `method.args`. ```R args <- list(formula = y ~ k * e ^ x, start = list(k = 1, e = 2)) ggplot(mtcars, aes(wt, mpg)) + geom_point() + stat_fit_augment(method = "nls", method.args = args) ``` -------------------------------- ### Linear Regression with Table Theme and Non-Default Position Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_tb.html Demonstrates linear regression using stat_fit_tb with a specified table theme (ttheme_gtlight) and non-default positioning for the table. Requires the 'broom' package. ```r if (broom.installed) ggplot(my.df, aes(covariate, x)) + geom_point() + stat_fit_tb(table.theme = ttheme_gtlight, npcx = "left", npcy = "bottom") + expand_limits(y = 35) ``` -------------------------------- ### Inspecting stat_quant_band Data with fm.values = TRUE Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_band.html Uses geom_debug_group with fm.values = TRUE to inspect detailed fitting information from stat_quant_band. Requires gginnards to be installed. ```R if (gginnards.installed) ggplot(mpg, aes(displ, hwy)) + stat_quant_band(geom = "debug_group", fm.values = TRUE) ``` -------------------------------- ### Pearson Correlation Test Statistics Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_glance.html Displays Pearson correlation coefficient and p-value. The 'broom' package must be installed. The formula is automatically extracted from the arguments. ```R if (broom.installed) ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() + stat_fit_glance(method = "cor.test", label.y = "bottom", method.args = list(formula = ~ x + y), mapping = aes(label = sprintf('r[Pearson]~"="~%.3f~~italic(P)~"="~%.2g', after_stat(estimate), after_stat(p.value))), parse = TRUE) ``` -------------------------------- ### Load Required R Packages Source: https://docs.r4photobiology.info/ggpmisc/articles/major-axis-regression.html Loads the necessary R packages for performing major axis regression and plotting. Ensure these packages are installed before running. ```R library(ggpmisc) library(smatr) library(lmodel2) ``` -------------------------------- ### Manually Assemble Label with paste() Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html Demonstrates manual assembly and mapping of a specific label using paste() and aes() for stat_quant_eq. This allows for custom formatting of the equation label. ```R ggplot(my.data, aes(x, y2, color = group, grp.label = group)) + geom_point() + stat_quant_line(method = "rq", formula = formula, quantiles = c(0.05, 0.5, 0.95), linewidth = 0.5) + stat_quant_eq(mapping = aes(label = paste(after_stat(grp.label), "*\": \"*", after_stat(eq.label), sep = "")), quantiles = c(0.05, 0.5, 0.95), formula = formula, size = 3) ``` -------------------------------- ### Multiple Quantiles with Numeric Output Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html This snippet demonstrates specifying multiple quantiles and retrieving the numeric coefficients for each. ```R if (gginnards.installed) ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_eq(formula = formula, quantiles = c(0.25, 0.5, 0.75), geom = "debug_group", output.type = "numeric") ``` -------------------------------- ### Fitting Higher-Order Polynomials with stat_poly_eq Source: https://docs.r4photobiology.info/ggpmisc/articles/user-guide.html Fit and display equations for higher-order polynomials using the poly() function in the formula. This example uses a 5th-degree polynomial. ```R formula <- y ~ poly(x, 5, raw = TRUE) ggplot(my.data, aes(x, y)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(mapping = use_label("eq"), formula = formula, size = 2.7) ``` -------------------------------- ### Polynomials using poly() function Source: https://docs.r4photobiology.info/ggpmisc/reference/check_poly_formula.html Demonstrates checking formulas that use the `poly()` function. It distinguishes between raw and orthogonal polynomials, with only raw polynomials yielding a TRUE result for label checking. ```r check_poly_formula(y ~ poly(x, 2, raw = TRUE)) # label o.k. #> [1] TRUE ``` ```r check_poly_formula(y ~ poly(x, 2)) # orthogonal polynomial -> bad label #> Warning: 'poly()' in model formula has to be passed 'raw = TRUE': 'eq.label' set to NA! #> [1] FALSE ``` -------------------------------- ### Combine other mappings with default labels using use_label Source: https://docs.r4photobiology.info/ggpmisc/reference/use_label.html Illustrates combining other aesthetic mappings (e.g., color) with default labels generated by `use_label()`. Requires artificial data and a formula definition. ```R set.seed(4321) x <- 1:100 y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4) my.data <- data.frame(x = x, y = y * 1e-5, group = c("A", "B"), y2 = y * 1e-5 + c(2, 0)) # give a name to a formula formula <- y ~ poly(x, 3, raw = TRUE) # combine other mappings with default labels ggplot(data = my.data, mapping = aes(x = x, y = y2)) + geom_point(mapping = aes(colour = group)) + stat_poly_line(mapping = aes(colour = group), formula = formula) + stat_poly_eq(mapping = use_label(aes(colour = group)), formula = formula) ``` -------------------------------- ### Fit Segmented Linear Model with Two Breakpoints Source: https://docs.r4photobiology.info/ggpmisc/articles/poly-advanced.html Use stat_poly_line with lm2segmented.lm to fit a segmented linear model with specified starting breakpoint values. ```r ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_poly_line(method = lm2segmented.lm, method.args = list(psi = c(3, 5))) ``` -------------------------------- ### No Weights, Quantile at Upper Boundary Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_eq.html Shows how to set the quantile to the upper boundary (0.95) when weights are not used. Includes a warning about non-positive fis. ```R ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_line(formula = formula, quantiles = 0.95) + stat_quant_eq(formula = formula, quantiles = 0.95) ``` -------------------------------- ### Fit Quantile Line using rqss (x ~ y) Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_quant_line.html This example demonstrates fitting a quantile smoothing spline using the 'rqss' method with x as the dependent variable. The formula 'x ~ qss(y, constraint = "D")' is used, and confidence intervals are automatically included by default when se is not specified. ```R ggplot(mpg, aes(displ, hwy)) + geom_point() + stat_quant_line(method = "rqss", formula = x ~ qss(y, constraint = "D"), quantiles = 0.5) ``` -------------------------------- ### Weighted Regression with R-squared and P-value Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_fit_glance.html Displays R-squared and p-value for a weighted linear regression model. Ensure the 'broom' package is installed and the 'weight' aesthetic is mapped. ```R if (broom.installed) ggplot(mtcars, aes(x = disp, y = mpg, weight = cyl)) + stat_smooth(method = "lm") + geom_point(aes(colour = factor(cyl))) + stat_fit_glance(method = "lm", label.y = "bottom", method.args = list(formula = y ~ x, weights = quote(weight)), mapping = aes(label = sprintf('r^2~"="~%.3f~~italic(P)~"="~%.2g', after_stat(r.squared), after_stat(p.value))), parse = TRUE) ``` -------------------------------- ### Display Quartile Band with ggpmisc Source: https://docs.r4photobiology.info/ggpmisc/articles/user-guide.html Use stat_quant_band to visualize the spread of observations between quartiles, similar to a box plot. This example fits a second-degree polynomial model. ```r ggplot(my.data, aes(x, y)) + geom_point() + stat_quant_band(formula = y ~ poly(x, 2)) ``` -------------------------------- ### Load necessary R packages and set xdvir engine Source: https://docs.r4photobiology.info/ggpmisc/articles/output-types.html Loads essential R packages for plotting and typesetting, including grid, ggpmisc, xdvir, ggtext, and marquee. It also explicitly sets the 'xdvir' engine to 'xetex' to avoid potential 'luatex' issues. ```R library(grid) library(ggpmisc) library(xdvir) library(ggtext) library(marquee) options(xdvir.engine="xetex") # luatex can fail ``` -------------------------------- ### Controlling R-squared Digits Source: https://docs.r4photobiology.info/ggpmisc/reference/stat_poly_eq.html Illustrates how to set the number of digits for the R-squared value in the displayed equation. Use rr.digits to control precision, for example, to 4 digits. ```R ggplot(my.data, aes(x, y)) + geom_point() + stat_poly_line(formula = formula) + stat_poly_eq(formula = formula, rr.digits = 4) ```