使用 facet_wrap 在 ggplot 中绘制全范围分位数回归
Plotting Quantile regression with full range in ggplot using facet_wrap
所以我想在使用 facet_wrap
时绘制全范围的整个全范围分位数线。代码如下:
library(tidyverse)
library(quantreg)
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")
#> [multiple warnings removed for clarity]
由 reprex package (v0.3.0)
于 2019-12-05 创建
可以看出回归线没有全范围,我无法轻易解决这个问题。
这感觉设计过度了,但一种方法是在 ggplot 之外获取斜率截距图,然后使用 geom_abline
绘制它们。此实现的一个潜在缺点是它使用一些抖动来防止 rq
中的 "singular design matrix" 错误,但这意味着即使对于只有一个 x 值的数据,它也会生成随机斜率。为了解决这个问题,如果该可变齿轮组合只有一个值,这里有一个步骤可以从斜率计算中删除数据。
mtcars %>%
gather("variable", "value", -c(3, 10)) -> mt_tidy
mt_tidy %>%
# EDIT: Added section to remove data that only has one value for that
# variable and gear.
group_by(variable, gear) %>%
mutate(distinct_values = n_distinct(value)) %>%
ungroup() %>%
filter(distinct_values > 1) %>%
select(-distinct_values) %>%
nest_legacy(-c(variable, gear)) %>%
# the jittering here avoids the "Singular design matrix" error
mutate(qtile = map(data, ~ rq(jitter(.x$disp) ~ jitter(.x$value),
tau = 0.5)),
tidied = map(qtile, broom::tidy)) %>%
unnest_legacy(tidied) %>%
select(gear:estimate) %>%
pivot_wider(names_from = term, values_from = estimate) %>%
select(gear, variable,
intercept = `(Intercept)`,
slope = `jitter(.x$value)`) -> qtl_lines
ggplot(mt_tidy, aes(value, disp, color = factor(gear))) +
geom_point() +
geom_abline(data = qtl_lines,
aes(intercept = intercept, slope = slope,
color = factor(gear))) +
facet_wrap(~variable, scales = "free")
所以我想在使用 facet_wrap
时绘制全范围的整个全范围分位数线。代码如下:
library(tidyverse)
library(quantreg)
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")
#> [multiple warnings removed for clarity]
由 reprex package (v0.3.0)
于 2019-12-05 创建可以看出回归线没有全范围,我无法轻易解决这个问题。
这感觉设计过度了,但一种方法是在 ggplot 之外获取斜率截距图,然后使用 geom_abline
绘制它们。此实现的一个潜在缺点是它使用一些抖动来防止 rq
中的 "singular design matrix" 错误,但这意味着即使对于只有一个 x 值的数据,它也会生成随机斜率。为了解决这个问题,如果该可变齿轮组合只有一个值,这里有一个步骤可以从斜率计算中删除数据。
mtcars %>%
gather("variable", "value", -c(3, 10)) -> mt_tidy
mt_tidy %>%
# EDIT: Added section to remove data that only has one value for that
# variable and gear.
group_by(variable, gear) %>%
mutate(distinct_values = n_distinct(value)) %>%
ungroup() %>%
filter(distinct_values > 1) %>%
select(-distinct_values) %>%
nest_legacy(-c(variable, gear)) %>%
# the jittering here avoids the "Singular design matrix" error
mutate(qtile = map(data, ~ rq(jitter(.x$disp) ~ jitter(.x$value),
tau = 0.5)),
tidied = map(qtile, broom::tidy)) %>%
unnest_legacy(tidied) %>%
select(gear:estimate) %>%
pivot_wider(names_from = term, values_from = estimate) %>%
select(gear, variable,
intercept = `(Intercept)`,
slope = `jitter(.x$value)`) -> qtl_lines
ggplot(mt_tidy, aes(value, disp, color = factor(gear))) +
geom_point() +
geom_abline(data = qtl_lines,
aes(intercept = intercept, slope = slope,
color = factor(gear))) +
facet_wrap(~variable, scales = "free")