gpt4 book ai didi

r - ggplot2 中的 geom_quantile 全范围

转载 作者:行者123 更新时间:2023-12-04 10:09:13 24 4
gpt4 key购买 nike

有没有办法在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com