Geom_Quantile Full Range in Ggplot2

Geom_Quantile Full Range in Ggplot2

Is there a way how to set full_range = T parametr somehow in ggplot?

library(ggplot2)
ggplot(mtcars, aes(hp, disp))  + 
  geom_point() + 
  #geom_smooth(method = "lm", aes(group = factor(gear), color = factor(gear)), fullrange = T)
  geom_quantile(quantiles = 0.5, aes(group = factor(gear), colour = factor(gear)), fullrange = T)

So the quantile regression line would be "as long" as when using geom_smooth above?

Is there a way how to make it work?

Also is there a way how to plot full range when using facet_wrap function

NEW MODIFIED QUESTION: Plotting Quantile regression with full range in ggplot using facet_wrap

for example say something like this:

mtcars %>% gather("variable", "value", -c(3, 10))%>% ggplot(aes(value, disp)) +
 geom_point(aes(color = factor(gear))) + 
geom_quantile(quantiles = 0.5, aes(group = factor(gear), color =factor(gear))) + facet_wrap(~variable, scales = "free")
0

1 Answer

I looked into StatQuantile$compute_group and it turns out you can specify the xreg argument as follows:

ggplot(mtcars, aes(hp, disp))  + 
  geom_point() + 
  geom_quantile(quantiles = 0.5, aes(group = factor(gear), colour = factor(gear)),
                xseq = min(mtcars$hp):max(mtcars$hp))

Result


This is the code

statQuantile$compute_group
<ggproto method>
  <Wrapper function>
    function (...) 
f(...)

  <Inner function (f)>
    function (data, scales, quantiles = c(0.25, 0.5, 0.75), formula = NULL, 
    xseq = NULL, method = "rq", method.args = list(), lambda = 1, 
    na.rm = FALSE) 
{
    try_require("quantreg", "stat_quantile")
    if (is.null(formula)) {
        if (method == "rqss") {
            formula <- eval(substitute(y ~ qss(x, lambda = lambda)), 
                list(lambda = lambda))
            qss <- quantreg::qss
        }
        else {
            formula <- y ~ x
        }
        message("Smoothing formula not specified. Using: ", deparse(formula))
    }
    if (is.null(data$weight)) 
        data$weight <- 1
    if (is.null(xseq)) { # <-------------------------------
        xmin <- min(data$x, na.rm = TRUE)
        xmax <- max(data$x, na.rm = TRUE)
        xseq <- seq(xmin, xmax, length.out = 100)
    }
    grid <- new_data_frame(list(x = xseq))
    if (identical(method, "rq")) {
        method <- quantreg::rq
    }
    else if (identical(method, "rqss")) {
        method <- quantreg::rqss
    }
    else {
        method <- match.fun(method)
    }
    rbind_dfs(lapply(quantiles, quant_pred, data = data, method = method, 
        formula = formula, weight = weight, grid = grid, method.args = method.args))
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.