ggplot2中的geom_quantile完整范围 [英] geom_quantile full range in ggplot2

查看:231
本文介绍了ggplot2中的geom_quantile完整范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在ggplot中以某种方式设置full_range = T参数?

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)

那么分位数回归线会和上面使用geom_smooth时一样长吗?

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

有办法使它工作吗?

还有使用facet_wrap函数时如何绘制全范围图的方法

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

新修改的问题:绘制分位数回归使用facet_wrap

例如说这样的话:

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")

推荐答案

我调查了StatQuantile$compute_group,事实证明您可以按如下方式指定xreg参数:

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))

结果

这是代码

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))
}

这篇关于ggplot2中的geom_quantile完整范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆