r - ggplot2 中的 geom_quantile 全范围

标签 r ggplot2 quantile

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

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 时“一样长”吗?

有没有办法让它发挥作用?

在使用facet_wrap 函数时,还有一种方法如何绘制全范围

新修改的问题:Plotting Quantile regression with full range in ggplot using 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 参数:

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

结果

enter image description here


这是代码

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

关于r - ggplot2 中的 geom_quantile 全范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184868/

相关文章:

R - 从一系列日期自动填充数据框中的列

r - 如何在不同的数据帧中选择特定时间段内的点,然后根据纬度/经度计算这两个点之间的距离

r - 如何添加线以将回归线上的点连接到 ggplot 上的 x 和 y 轴?

重新排序因子水平以在多面包裹条形图中正确显示

python - 使用 resample 和 groupby - pandas 计算时间序列的百分位数/四分位数

javascript - 使用不同颜色(不是渐变)为 SVG 路径着色的有效方法

r - ggplot2 更改每个单独方面面板的轴限制

r - 加速分位数计算

postgresql - 如何在 Postgres 中使用具有多个分位数的 percentile_conts

r - 如何在函数内将对象变量转换为字符串