geom_quantile ggplot2 中的完整范围
geom_quantile full range in ggplot2
有没有办法在 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)
所以分位数回归线将是 "as long",就像上面使用 geom_smooth 时一样?
有没有办法让它发挥作用?
还有一种方法可以在使用 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))
结果
这是代码
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))
}
有没有办法在 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)
所以分位数回归线将是 "as long",就像上面使用 geom_smooth 时一样?
有没有办法让它发挥作用?
还有一种方法可以在使用 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))
结果
这是代码
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))
}